bool correct = true;
bool wrong = false;
double radius;
do
{
cout << "Enter the radius of a circle: ";
cin >> radius;
}
while (wrong == false);
{
if (radius >= 0 || radius <= 9)
{
wrong = true;
}
else
{
cout << "Enter a NUMBER please!";
}
}
There is my code. The problem that I am having is that it is stuck in an infinite loop. All I am trying to do is when the User enters a number, I check if it is a number, if not, it will ask again. Any Help?
bool correct = true;
bool wrong = false;
double radius;
do
{
cout << "Enter the radius of a circle: ";
cin >> radius;
// I don't know how to read your validation of the number here...
if (radius >= 0 || radius <= 9)
{
wrong = true;
}
else
{
cout << "Enter a NUMBER please!";
}
}
while (wrong == false);
bool correct = true;
bool wrong = true; // lets assume the number will be wrong first
double radius;
do
{
cout << "Enter the radius of a circle: ";
cin >> radius;
if(cin.fail()) // see if we entered a valid double.
{
cout << "Enter a NUMBER please!" << endl;
}
else
{
wrong = false;
}
}
while (wrong == true);
Aside from what I already posted, your problem is that you set wrong as false: bool wrong = false;
And then in your loop, you went ahead and told it to keep going until wrong is true, but you failed to actually make wrong true until after the execution of the loop.
1 2 3 4 5 6
do{
cout << "Enter the radius of a circle: ";
cin >> radius;
if(radius >= 0 || radius <= 9){wrong = true;}
else {cout << "Enter a numerical radius: ";}
}while(wrong == false);
This code is still bad, but the flow of logic has been corrected.