If while loop won't end


When I run this it gets the sorting right but it runs though each one and I can't make it stop when it is correct. This is driving me crazy any help is appreciated. Thanks



#include <iostream>
#include <string>

using namespace std;

int main()
{// declaring variables//
int userInput;

cout << "Enter a number between 0 and 36 ."endl;
cin >> userInput;

//validating user input within range.//
while (userInput > 36)
{
cout << "\n";
cout << " !ERROR!: You must enter a number between 0 and 36" << endl;
cin >> userInput;
}
cout << "\n";
{


while (userInput >=1||userInput <=10)
if (userInput % 2 == 0)
{
cout << "You rolled Black!!";
}
else
{
cout << "You rolled Red!!";

break;
}//end if
while (userInput >=11||userInput <=18)
if (userInput % 2 == 0)
{
cout << "You rolled Red!!";
}
else
{

cout << "You rolled Black!!";
break;
//end if
while (userInput >=19 ||userInput <= 28)
if (userInput % 2 == 0)
{
cout << "You rolled Black!!";
}
else
{
cout << "You rolled Red!!";
break;
//end if
while (userInput >=29 ||userInput <= 36)
if (userInput % 2 == 0)
{
cout << "You rolled Red!!";
}
else
{
cout << "You rolled Black!!";
break;
if (userInput == 0)
{
cout << "You rolled Green!!";
}
//end if









system("pause");
return 0;
All numbers are >= 1 or <= 10. No number is simultaneously greater than 10 and less than 1.
Same for 11 and 18; 19 and 28; 29 and 36.
Last edited on
Your while loops can be greatly simplified. As @helios said, you're basically separating segments of your input for no reason. Also think about what happens when the user enters a number, like say... (-1).

Also try not to use system(anything). Your program has no reason to call upon the operating system to shut it down.
use && rather than ||
Topic archived. No new replies allowed.