while loop problem
May 23, 2016 at 12:31pm UTC
Hi guys,I want my program to exit while the var TheChoice is not = to 4 but what I find is after I'm well into the loop when I hit 4 the loop does not exit,it continues any idea what I did wrong in my code? thanks
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
#include <iostream>
using namespace std;
int getChoice();
int main()
{
int theChoice = getChoice();
while (theChoice != 4){
int theChoice = getChoice();
}
}
int getChoice(){
int choice;
cout << "enter 1 for useless items" << endl;
cout << "enter 2 for helpful items" << endl;
cout << "enter 3 for harmful items" << endl;
cout << "enter 4 to quit" << endl;
cin >> choice;
return choice;
}
May 23, 2016 at 1:05pm UTC
Line 13: You're declaring another instance of theChoice with block scope. This hides the variable at line 9. Remove the int
from line 13.
May 23, 2016 at 2:06pm UTC
yeah that was driving me crazy something so simple haha
Topic archived. No new replies allowed.