do while.... What the??

Why the heck does my while here think that my do input is always negative?????????
1
2
3
4
5
6
7
8
9
10
11
12
13
  int main()
{
	int n;
	
	do	{
		cout << "Enter a non-negative integer: ";
		cin >> n;
	} while (n < 0);
			cout << "The integer you entered is negative." << endl;
		cout << "Enter a non-negative integer: ";
		cin >> n;
	
}
Last edited on
Why do you have lines 9-11?
Last edited on
The loop condition tests if the input is negative so that means the loop will run as long as you input negative numbers. If you input a positive number the loop will end and the code after the loop will run but it lies to you, claiming you entered a negative number when you didn't.
Last edited on
yes but what is happening.. is I enter a positive integer... and it loops into my while loop stating that the integer I entered is negative (when it clearly is not)
this is part of an assignment... converting a simple statement into a do while loop. The loop runs until a positive integer is entered... but what happens is I enter a positive integer, and it thinks it's a negative...
From the (inconsistent) indentation of line 9 and the text that accompanies your code, I believe you're under the mistaken assumption that line 9 is controlled by the while. It is not. Line 9 will always execute.
any ideas?
do..while is not ideal for that. A plain while would be better. You can use do..while, but you would need additional conditional within the loop.
the assignment calls for the do while though :(
ah I see that now cire... I'm trying to reformat the code... and it looks like it's impossible with a do.. while.. since the do always executes once... it never makes sense...

int main()
{
int n;
cout << "Enter a non-negative integer: ";
cin >> n;
do {
cout << "The integer you entered is negative." << endl;
cout << "Enter a non-negative integer: ";
cin >> n;

} while (n < 0);


}


Here's the exact problem as worded by my professor:

"Rewrite the following code fragment so that it uses a "
do...while..." loop to accomplish the same task.
int n;
cout << "Enter a non
-
negative integer: ";
cin >> n;
while (n < 0)
9
{
cout << "The integer you entered is negative." << endl;
cout << "Enter a non
-
negative integer: ";
cin >> n;
}

Write, run and execute and screen capture the results of a program that utilizes the above code to show your results.
"
Last edited on
Capitalizing on the fact that an the expression cout << "literal" is convertible to bool (and for practical purposes always evaluates to true,) we might use something like the following:

1
2
3
4
5
6
7
8
9
int main()
{
	int n;
	
	do	{
		cout << "Enter a non-negative integer: ";
		cin >> n;
	} while (n < 0 && cout << "The integer you entered is negative.\n" );	
}


I doubt, however, that it is the solution expected by your instructor.
that works, but I tend to agree, I'm not sure that's what he's looking for. I've never seen a cout inside a while expression like that before.
Topic archived. No new replies allowed.