Question

I was trying to make a bank program in C++, where you can view the amount of money in your wallet, and in your bank account, and deposit and withdraw money. when I try to run it, it lets me input, but when i input anything, it repeats Money in Wallet: 20 again and again and again until I get stack overflow, which I thought was kind of ironic, posting this on a website called Stack Overflow.

This is my code so far:

#include <iostream>
using namespace std;

float money= 20.00;
float account=100.00;
float amount;
bool cmd;

void wallet()
{
    cout<<"Money in Wallet: "<<money<<endl;
}


void bank()
{
    cout<<"Money in Bank: "<<account<<endl;
}


void deposit()
{
    cout<<"How much do you want to deposit?: ";
    cin>>amount;
    if (money>=amount)
    {
        account = account+amount;
        money = money-amount;
    }
    else
    {
        cout<<"You don't have enough money!\n";
    }
}


void withdraw()
{
    cout<<"How much do you want to withdraw?: ";
    cin>>amount;
    if(account>=amount)
    {
        money = money+amount;
        account = account-amount;
    }
    else
    {
        cout<<"There isn't enough money in your bank account!\n";
    }
}


void prompt()
{
    cmd="null";
    cout<<">";
    cin>>cmd;
    if (cmd="wallet")
    {
        wallet();
        cmd="null";
        prompt();
    }
    else{
        cout<<"Unknown Command.";
        cmd="null";
        prompt();
    }
    if (cmd="bank")
    {
        bank();
    }
    else{cout<<"Unknown Command.";}
    if (cmd="deposit")
    {
        deposit();
    }
    else{cout<<"Unknown Command.";}
    if (cmd="withdraw")
    {
        withdraw();
    }
    else{cout<<"Unknown Command.";}
}


int main()
{
    prompt();
}

Please help!

Was it helpful?

Solution

I can see multiple issues with your code.

First, you are calling the function prompt() inside the function causing it to recursively call it self over and over until you have a stackoverflow.

Second, your storing your input inside of a bool. You probably meant to use std::string

Third, inside of your if statement you are not checking if the value is equal but setting it when you use the = operator. If you want to check if the two values are equal use the == sign.

Fourth, your use of the if statement is wrong. If you are checking a value for multiple conditions you should place them all inside if else all following each other. What you are doing is spearting them into multiple if statements each following an else condition that prints Unknown Command.]

Seeing as how this looks homework, I won't give you the full code to give you a chance to learn. But if you fix the points above then you will be on your way to having a functional program.

OTHER TIPS

Your structure needs to be more like this

while(true)
{
  prompt();
  if(command == "quit")
     break;
  else if(command == "bank")
    bank();
  else if(.....)
....
  else
    cout << "bad command"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top