1

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!

  • 3
    why does every professor in the world assign his CS 101 students to design a bank? i hope this is not how my bank's backend was made. – Andrey Mishchenko Jan 04 '14 at 00:27
  • 1
    If you stepped it with a debugger you would find it in about 30 seconds. – Rob Jan 04 '14 at 00:30
  • You're probably either looping infinitely or recursively calling a function infinitely. Why don't you try stepping through it with a debugger to find out where the problem is? – derpface Jan 04 '14 at 00:31
  • Testing for equality is done with double equals (==), not with single equals (=). – jarmod Jan 04 '14 at 00:32
  • Use better names for your variables and methods. For example, 'account' is a very bad name for an account balance, bank() is a very bad name for a method that prints out an account's balance. Why would a deposit() be rejected with "You don't have enough money!"? – jarmod Jan 04 '14 at 00:34
  • @andrey because the bank account problem is concrete. It's good to start with programming. That said, the banking problem can be applied to almost any aspect of programming like thread safe code, database, data structures, sockets and so on. In the end the difference between a CS101 program and a real banking system is the same a the difference between a buggy and a car. – Loïc Faure-Lacroix Jan 04 '14 at 01:14
  • My first comment is **stop storing money in floating-point values!!!!** Argh. Used fixed-point. In C++ that means integers divided by 100, i.e. penny units. – Lightness Races in Orbit Jan 04 '14 at 02:12

2 Answers2

6

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.

Caesar
  • 9,483
  • 8
  • 40
  • 66
0

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"
}
pm100
  • 48,078
  • 23
  • 82
  • 145