Noob WHILE loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	
        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?
Are you sure do while loops work like that?
your code should look like this instead of what you have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        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);


but I probably would do this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        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);

oh i think i might know what is wrong try this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 


        do
	{
		cout << "Enter the radius of a circle: ";
		cin >> radius;


       if (radius >= 0 || radius <= 9)
       wrong = true;
		
}

	while (wrong == false);
	
		
	
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.
awwww yeaaah, thanks guys and gals!
Topic archived. No new replies allowed.