SiliconArmour
SiliconArmour

Reputation: 55

Loop going through incorrectly in C++

So I wrote this code:

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

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

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)
    {
        while(true)
        {
            cout << "Would you like to make a withdrawal, a deposit, or quit the program: ";
            cin >> choice;
            if(choice.compare("withdrawal") == 0 || choice.compare("w") == 0)
            {
                cout << "Please enter the amount to withdraw: ";
                cin >> withdraw;
                bankObject.withdrawMoney();
                cout << "New balance is: $" << balance << endl;
                break;
            }
            else if(choice.compare("deposit") == 0 || choice.compare("d") == 0)
            {
                cout << "Please enter the amount to deposit: ";
                cin >> deposit;
                bankObject.depositMoney();
                cout << "New balance is: $" << balance << endl;
                break;
            }
            else if(choice.compare("quit") == 0 || choice.compare("q") == 0)
            {
                break;
            }
            else
            {
                cout << "Invalid input." << endl;
                break;
            }
        }
        if(choice.compare("quit") == 0)
        {
            break;
        }
        cout << "Would you like to try again or quit: ";
        cin >> quitOrNo;
        if(quitOrNo.compare("quit") == 0)
        {
            break;
        }
    }
    cout << "Thank you for using the Bank Program." << endl;
    return 0;
}

When I try to run the code, I get this output: --> Welcome to the Bank Program! Would you like to make a withdrawal, a deposit, or quit the program: deposit Please enter the amount to deposit: 100 New balance is: $100 Would you like to try again or quit: try again Would you like to make a withdrawal, a deposit, or quit the program: Invalid input. Would you like to try again or quit: <-- It will continue doing this until I just exit the program. Anyone know why? The bold text is the input from me and the italicized text is the input the computer gave itself. Everything in arrows is the console text.

Upvotes: 2

Views: 227

Answers (2)

paddy
paddy

Reputation: 63471

The problem is you're reading a string, rather than a whole line. String input is delimited by whitespace, not by end of line.

Because you typed try again, the value read into quitOrNo will be try, leaving the text again in the stream. When you loop around and read into choice, you will get again which is invalid. Because you keep typing the same thing, you keep getting the same problem.

You should look into using std::getline instead.

Upvotes: 4

Potatoswatter
Potatoswatter

Reputation: 137810

try again is two words but >> applied to a std::string only extracts one word. The again gets extracted by the next loop iteration. Use getline to extract exactly one line of input.

I'm not sure if or how it's going into an endless loop, but if cin can't process some input it's been given, it will stop accepting any more input until you call cin.clear(). In the meantime a statement if (cin) will see cin evaluate to false. A program lacking such a check will usually read infinite invalid input by default.

Upvotes: 4

Related Questions