Do while loop ignoring 2nd else if

Hey guys,
I wrote this code and it compiles but when I go to test it, it does not do any of the options except withdraw and ignores the while loop. Any ideas on how I could fix this? Let me know.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace std;

int main()
{
double initialAccount = 5000.00 ;
double accountBalance = initialAccount ;
string userInput;
double withdrawalAmount;
double depositAmount;

cout << "******Welcome to your neighborhood ATM******" << endl;
cout << "*Please choose one of the following choices*" << endl;
cout << "*******Withdrawal" << endl;
cout << "*******Deposit" << endl;
cout << "*******Show Balance" << endl;
cout << "*******Quit" << endl;
cin >> userInput;


do
{
   if (userInput == "w" || "W" || "w*" || "W*")
   {
     cout << "Please enter the amount you wish to withdraw" << endl;
     cin >> withdrawalAmount;
     accountBalance = initialAccount - withdrawalAmount;
     cout << "Your new balance is " << accountBalance << endl;
     }
   else if (userInput == "d" || "D" || "d*" || "D*")
   {
     cout << "Please enter an amount to be deposited"
               "into your account" << endl;
     cin >> depositAmount;
     initialAccount = initialAccount + depositAmount;
     cout << "Your new balance is " << accountBalance << endl;
     }
   else if (userInput == "b" || "B" || "b*" || "B*" || "s" || "S" || "s*" || "S*")
   {
     
     cout << "Your account balance is " << accountBalance << endl;
     }
   else if (userInput == "q" || "Q" || "q*" || "Q*")
   {
     cout << "Thank You for choosing us to bank with." << endl;
     cout << "Have an amazing day!" << endl;
     }
   else
   {
     cout << "Your input is not recognized, please enter "
     "one of the following choices." << endl;
     cout << "*******Withdrawal" << endl;
     cout << "*******Deposit" << endl;
     cout << "*******Show Balance" << endl;
     cout << "*******Quit" << endl;
     }
     }
while (userInput != "q" || "Q" || "q*" || "Q*" );
}
Your if statement construction is wrong. You need to form it as:

 
if (userInput == "b" || userInput == "B" || userInput == "b*")


Same with the while, except that'll be:

 
while (userInput != "q" && userInput != "Q" && so on);


What you had was comparing userInput with "b" and then using "B" as a boolean value, which being non-zero would equate to true. So your if was saying, "if (userInput == "b" || true)"

Cheers,
Jim
Also, is "q*" an actual expected option, or are you trying to check that the string starts with q?

If it's the latter, that won't work - it's comparing the literal string "q*", not applying wildcards.

Try using, userInput[0] to get at the first character of the string (but make sure you check it has at least one character, or you'll get an exception - use userInput.length()

Jim
I think your code would be much better with this outer loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char userInput; //not string
bool Quit = false;

while(!Quit) {
ShowMenu(); //Show the menu text
cin >> userInput;
userInput = toupper(userInput); //reduce the testing

switch (userInput)
     case: 'W'
            Withdraw(); //call the Withdraw function
             break;
     case: 'D'
            Desposit()
             break;
      case: 'B'
             ShowBalance();
             break;
       case: 'Q'
             Quit = true;
       default:
             std::cout << "Invalid input" << std::endl;
             break;
}


You can use a combination of Jim's suggestion & my code to make it a little more robust.

HTH
In this statement

if (userInput == "w" || "W" || "w*" || "W*")

expression

userInput == "w" || "W" || "w*" || "W*"

is always equal to true because for example subexpression "W" that has type const char * is not equal to zero.

Topic archived. No new replies allowed.