Need help on fixing a loop.

Hello. I am currently working on a project for a class and I need help trying to figure out why when I input an invalid command in my code it wont leave the loop I have it in when the command valid.
I've been working on this for the past couple of hours and I can't figure out why it wont leave the loop. Any ideas? Thanks.

The code in it's entirety, the loop were I am having trouble in is at the bottom:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string altpass = "altpass";
    string command = "";
    string option = "";
    string check = "checkings";
    string save = "savings";
    string wthdl = "withdrawl";
    string logout = "logout";
    string depos = "deposit";
    string cancel = "cancel";
    string yes = "y";
    string no = "n";
      
    float chkaccntbal = 2000; //checkings, fix this later. only have it at 2000 for math testing purposes.
    float svaccntbal = 2000; //savings, fix this later. only have it at 2000 for math testing purposes.
    float withdrawl = 0;
    float deposit = 0;
    
    int sectic = 0; //security tics
	
    cout << "Welcome to your account Mr. Johnson, \n" << endl;
    cout << "You currently have $" << chkaccntbal  << " in checkings." << endl;
    cout << "You currently have $" << svaccntbal  << " in savings. \n" << endl;
    cout << "Do you wish to deposit, withdrawl, or logout?" << endl;
    cin >> command;
 
    //withdrawl
    while (command == wthdl)
    {
       cout << "Would you like to withdrawl from checkings or savings?" << endl;
       cin >> option;
       if (option == check)
       {
       cout << "How much would you like to withdrawl from checking?" << endl;
       cin >> withdrawl;
       while (withdrawl > chkaccntbal)
       {
       cout << "Note: you are withdrawling more than what you have in your account." << endl;
       cout << "Please enter a value less than or equal to $" << chkaccntbal << endl;
       cin >> withdrawl;
       }
       if (withdrawl >= 1000)
       {
       cout << "Please enter your alternate password to complete the transaction." << endl;
       cin >> altpass;
         while (altpass != "altpass")
         {
         cout << "Incorrect alternate password, please try again." <<endl;
         sectic = sectic + 1;
         cin >> altpass;  
           if (sectic == 3)
           {
           cout << "Password inputed wrong too many times, ending system for security purposes" << endl;
           cout << "Freezing account, please contact your local BoA to unfreeze account." << endl;
           system ("pause");
           return 0;   
           }
         }
       }
       chkaccntbal = chkaccntbal - withdrawl;
       cout << "You withdrew $" << withdrawl << " from checkings." << endl;
       cout << "You currently have $" << chkaccntbal << " in checkings." << endl; 
       cout << "Do you wish to deposit, withdrawl, or logout?" << endl;
       cin >> command;
       }
       if (option == save)
       {
       cout << "How much would you like to withdrawl from savings?" << endl;
       cin >> withdrawl;
       while (withdrawl > svaccntbal)
       {
       cout << "Note: you are withdrawling more than what you have in your account." << endl;
       cout << "Please enter a value less than or equal to $" << svaccntbal << endl;
       cin >> withdrawl;
       }
       if (withdrawl >= 1000)
       {
       cout << "Please enter your alternate password to complete the transaction." << endl;
       cin >> altpass;
         while (altpass != "altpass")
         {
         cout << "Incorrect alternate password, please try again." <<endl;
         sectic = sectic + 1;
         cin >> altpass;  
           if (sectic == 3)
           {
           cout << "Password inputed wrong too many times, ending system for security purposes" << endl;
           cout << "Freezing account, please contact your local BoC to unfreeze account." << endl;
           system ("pause");
           return 0;   
           }
         }
       }
       svaccntbal = svaccntbal - withdrawl;
       cout << "You withdrew $" << withdrawl << " from savings." << endl;
       cout << "You currently have $" << svaccntbal << " in savings." << endl; 
       cout << "Do you wish to deposit, withdrawl, or logout?" << endl;
       cin >> command;
       }
    }
    
    //deposit
    while(command == depos)
    {
       cout << "Would you like to deposit into checkings or savings?" << endl;
       cin >> option;
       if (option == check)
       {
       cout << "How much would you like to deposit into checking?" << endl;
       cin >> deposit;
       chkaccntbal = chkaccntbal + deposit;
       cout << "You deposited $" << deposit << " into checkings." << endl;
       cout << "You currently have $" << chkaccntbal << " in checkings." << endl; 
       cout << "Do you wish to deposit, withdrawl, or logout?" << endl;
       cin >> command;
       }
       if (option == save)
       {
       cout << "How much would you like to deposit into savings?" << endl;
       cin >> deposit;
       svaccntbal = svaccntbal + deposit;
       cout << "You deposited $" << deposit << " into savings." << endl;
       cout << "You currently have $" << svaccntbal << " in savings." << endl; 
       cout << "Do you wish to deposit, withdrawl, or logout?" << endl;
       cin >> command;
       }
       
    }
    //logout
    if(command == logout)
    {
    cout << "Are you sure you want to logout? (y/n)" << endl;
    cin >> option;
      if (option == yes)
      {
      cout << "Logging out. Have a nice day." << endl;
      system ("pause");
      return 0;   
      }
      if (option == no)
      {
      cout << "Do you wish to deposit, withdrawl, or logout?" << endl;
      cin >> command;
      }
    }
while(command != depos || command != wthdl || command != logout)  
{
cout << "Invalid command. Please try again." << endl;
cin >> command;
}

system ("pause");
return 0;   
}
D3ATH94, im no expert, but one of your problems might be that when you enter inapropriate input, for example if you enter a char when cin is expecting a float, cin will produce an error, and in the past ive also found it difficult to get it to work after wierd input.
in the programing class im taking, our profesor has just shown us a method for dealing with this.

right after your input you want to 'flush' the cin buffer :

int variable;
cout << "Enter a Number : ";
cin >> vairable;
cin.ignore(999, '\n'); // this 'empties' cin you should use this after every input.

if ( cin.fail() ) // this function yeilds 1 for cin failure, and 0 for cin success
{
cin.clear(); // if cin fails, it wont work right untill you 'clear' the error
cin.ignore(); // then, once again, make sure there is no junk in there
}

you should do this for every input
closed account (2UD8vCM9)
Guessing your issue is with this
1
2
3
4
5
while(command != depos || command != wthdl || command != logout)  
{
cout << "Invalid command. Please try again." << endl;
cin >> command;
}


Think about what you're saying in this condition

This loop will run as long as one of the following are true
user input isn't deposit
user input isn't withdrawl
user input isn't logout

This is flawed logic. This loop will never end, because you are using the || OR operator where you should be using the && and operator. You only want to do these things if your response is not equal to any of those things.

Fix:
1
2
3
4
5
while(command != depos && command != wthdl && command != logout)  
{
cout << "Invalid command. Please try again." << endl;
cin >> command;
}


Also, it isn't withdrawl, it's spelled withdrawal.

Let me know if this helps or not. :)

More Detailed Explanation of why logic is flawed:
The loop will check each condition, and only needs one to be true to stay in the loop since you used the or operator ||

Let's say you input checking
is input != depos ? yes
is input != check ? yes
is input != withdrawl? no

We only need 1 yes to stay in the loop, so that is why you should use the && operator instead to make sure that all of your conditions you're checking are true.

Last edited on
Fix:
1
2
3
4
5
while(command != depos && command != wthdl && command != logout)  
{
cout << "Invalid command. Please try again." << endl;
cin >> command;
}


That sort of fixed it. While I can leave the loop now any time a correct command is inputted but now it just ends the program whenever it is inputted.
closed account (2UD8vCM9)
Well if you want your program to go back to the beginning after that code, you should set up a while loop.

Try this.
In the original code, at line 25 put
 
while (true) {


At line 156, put a } to close the while loop.
Try searching on this site for TheIdeasMan bool controlled while loop

switch statements are much better than this type of thing:

1
2
3
4
5
while(command != depos && command != wthdl && command != logout)  
{
cout << "Invalid command. Please try again." << endl;
cin >> command;
}


Also the code would greatly benefit from some use of functions. For example each case in a switch could call a function.

And try to avoid infinite loops, unless you really need them: that is when there are complex exit , initialisation, or incrementing conditions.

Hope all goes well.
Thanks Pindrought, that fixed it. And thank you everyone else for other solutions.
Topic archived. No new replies allowed.