+ 1

In the short code below, I declared an array of five elements. But I'm getting confused on how to use it in a while loop. My main question is , if the user inputs a userchoice1 value that is less than 1, I want the code to keep looping until the user inputs a number that is not less than 1. But I don't know if the 7th line (userchoice1 [x]++) shows the right way to loop it.

int userchoice1[5]; cin>>userchoice1 [x]; if (userchoice1[x]<1) { while (userchoice1 [x]<1){ cout<<"Number must not be less than 1" <<endl; cin>>userchoice1 [x]; userchoice1 [x]++; } }

29th Dec 2016, 4:59 PM
Cody Arthur
Cody Arthur - avatar
2 Answers
+ 1
thanks vishal. But I made a little mistake in my post. I have corrected it by also putting the cin statement before the if statement. And as for the array stuff, this code is a part of a much larger code which really wouldn't be necessary to post the whole thing. really all I need to know is how to loop it correctly(that is, if I got the last line)
29th Dec 2016, 7:49 PM
Cody Arthur
Cody Arthur - avatar
0
I am not sure why you are using array for a single input but basically: In your code the if statement will be false before even getting input so you dont need it. While will take care of checking input. while (input>1) { cout << "Enter input" << endl; cin >> input; ++input; } while (input>1) means loop until user has inputted more than 1. Use ++input and not input++ as the later skips one update.
29th Dec 2016, 6:38 PM
Vishal++
Vishal++ - avatar