C++ simple question

replace this do-while loop with a while loop. When u do this you will no longer need an if statement.

cout << "Enter an even number: ";
do
{ cin >> number;
if (number % 2 !=0)
cout << "Number must be even. Reenter number: ";
} while (number % 2 !=0);



THANKS--
try.
i did try many times bro it doesn't work
i did try many times bro it doesn't work
Where's the code that "doesn't work"?
1
2
3
4
5
6
7
8

cout << "Enter an even number: ";
do
{ cin >> number;
if (number % 2 !=0)
cout << "Number must be even. Reenter number: ";
} while (number % 2 !=0);
You didn't replace the do-while with a while loop.
i know that is why im asking you to do it because when i tried it doesn't work
We need to see the code you wrote that doesn't work so we can help you fix it. If you post it, we can help you make it work.
This is the syntax for a while loop:

1
2
3
4
while (condition)
{
     // code
}


I don't understand what you can't figure out.
Last edited on
please answer it i dun hv time
Apparently you dun hv time to spell out your words either. The only way I could make it simpler is by giving you the answer.

[Edit]: Give it your best attempt and post it.
Last edited on
then tell me
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <iostream>
using namespace std;

int main()
{
	int number;
		cout << "Enter an even number: ";
		while (number % 2 != 0);
		{	cin >> number;
		
			cout << "Number must be even. Reenter number: "; }
return 0;
}



here is the code now answer
This is very close. Add cin >> number; between lines 8 and 9, and switch lines 10 and 12.
problem 1. ';' after while(). That just creates an empty loop.
problem 2. comparison happens before input. One way would be to have cin >> number once before the loop and once in it (after cout << ... ). Another one would be while(cin >> number && number %2 != 0){...
Topic archived. No new replies allowed.