SiliconArmour
SiliconArmour

Reputation: 55

C++ if statement in function

So I am trying to write a basic program that will keep track of a balance, and you can make a withdraw, make a deposit, and quit the program altogether. This is the code.

#include <iostream>
#include <string>
using namespace std;

double balance = 0, withdraw = 0, deposit = 0;
string choice;

class Bank
{
    public:
        void withdrawMoney()
        {
            if(balance - withdraw >= 0)
            {
                balance = balance - withdraw;
            }
            else
            {
                cout << "$5 penalty for attempting to withdraw more than you have.";
                balance -= 5;
            }
        }
    public:
        void depositMoney()
        {
            balance = balance + deposit;
        }
};
int main()
{
    Bank bankObject;
    cout << "Welcome to the Bank Program!" << endl;
    while(true)
    {
        cout << "Would you like to make a withdrawal, a deposit, or quit the program: ";
        cin >> choice;
        if(choice.compare("withdrawal") == 0)
        {
            cout << "Please enter the amount to withdraw: ";
            cin >> withdraw;
            bankObject.withdrawMoney();
            cout << "New balance is: $" << balance << endl;
        }
        else if(choice.compare("deposit") == 0)
        {
            cout << "Please enter the amount to deposit: ";
            cin >> deposit;
            bankObject.depositMoney();
            cout << "New balance is: $" << balance << endl;
        }
        else if(choice.compare("quit") == 0)
        {
            break;
        }
        else
        {
            cout << "Invalid input." << endl;
        }
        cout << "Would you like to try again or quit: ";
        cin >> choice;
        if(choice.compare("quit") == 0)
        {
            break;
        }
    }
    cout << "Thank you for using the Bank Program." << endl;
    return 0;
}

I am new to C++, (I started today), but I have previous experience with Java. I am continually getting an error at the withdraw method, at the first if statement, saying that I have an invalid use of the member function. Any help would be appreciated. Also, not sure if it matters, but I am using the IDE Code::Blocks.

EDIT: First problem fixed, but now there is another, when I run the code, I can get through it once just fine, but when I try to go through a second time by typing try again, it gets stuck in a loop and answers the first question as "incorrect input". Help?

Upvotes: 0

Views: 3516

Answers (1)

Jim Lewis
Jim Lewis

Reputation: 45115

You use withdraw as both a global variable and a method name. Renaming one of them should fix this particular error.

Edit: When you type "try again", your program only reads "try" (because the default is to break up the input by whitespace), leaving "again" in the buffer for the next read from cin. You can verify that with a few debugging output statements.

Upvotes: 3

Related Questions