validation loop trouble
Oct 27, 2013 at 11:13pm UTC
The problem I am getting now is that I can input random things the 1st time and it works right but on the 2nd try it will take wrong inputs. EX: 1st input chess, it will say try again. 2nd input chess it will accept chess and open checking then put the h into deposit amount causing an error try again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
cout << "Checking (C) or Savings (S): enter your choice" << endl;
cin.get(choice);
//input validation
while (choice != 'c' && choice != 'C' && choice != 's' && choice != 'S' )
{
cin.clear();
cin.ignore(100, '\n' );
cout << "Invalid input. Try again: " ;
cin.get(choice);
}
//deposit in checking
if (choice == 'c' || choice == 'C' )
{
cout << "how much are you depositing: $" << endl; //get deposit
cin >> deposit;
while (!(cin >> deposit)) //make sure user input is valid
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n' );
cout << "Invalid input. Try again: " ;
}
if (deposit <= 0)
cout << "error: you cannot enter a negative or 0 amount" << endl;
else if (deposit > 0)
cout << "new checking balance: $" << checking + deposit << endl;
else
cout << "error program terminated" << endl;
}
Topic archived. No new replies allowed.