Help with while loop

Hello, I have a problem dealing with while loops.

I have to use the while loop to accomplish the following "Using a while loop - ask the user for integers; when user enters zero (0) stop . For each non-zero number, count how many even, odd, positive, and negative values are entered."

So far I have tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int main ()
{
	int num, pos=1, neg=1, even=1, odd=1;
	cout << "Please enter a number (0 to exit): ";
        cin >> num;
	while (num != 0)
	{
		cout << "Please enter a number (0 to exit): ";
        cin >> num;
		if (num=(num %2 == 0))
			++even;
		else if (num=(num %2 != 0))
			++odd;
		else if ((num > 0)&&(num !=0)) 
			++pos;
		else if ((num < 0) && (num !=0))
			++negl;
	}
	cout << " " << even << " even numbers"<< endl;
	cout << " " << odd << "  odd numbers"<< endl;
	cout << " " << pos << " positive numbers"<< endl;
	cout << " " << neg << " negative numbers"<< endl;
}


Completely lost, any advice on how to fix my many errors? Thank you in advance.
Last edited on
closed account (zb0S216C)
Personally, I would initialize all of your variables to zero, since initializing them to one assumes that one of each conditions were true, which may be a disadvantage in the long run.

Your if statements aren't quite right. The num= part of each statement is assignment, not an equality comparison. Equality comparison takes the form of ==. Even better, the assignment can be omitted all together. For example:

if( ( num % 2 ) == 0 )

Note that using parentheses within an expression overloads the precedence of the operators used in an expression. In the expression in my if statement, the compiler is forced to compute the expression ( num % 2 ) before testing for equality. The result of an expression is dependent on how the expression is structured.

I'd recommend revising each of your if/else if statements and take what I just said into consideration.

Wazzak
Last edited on
To add to what Framework already said, in your if/else if block of statements, your posTotal and negTotal will never be increased, because every number is going to be either even or odd. Once either of those is true, the program will skip the rest of the else if statements.

You can fix this by removing the "else" from your second else if statement. Then you'll have two seperate blocks of if/else if statements, and it will work the way you're intending.
Thank you both I am going to try your suggestions and update the code, I appreciate your help.
Hello I'm still having no luck with this one how would you go about writing this one to show the totals of even,odd, positive and negative numbers?
Could you post your updated code and the output you're getting?
Hello I had the configuration wrong for the if else statements as noted so I changed them to this.
1
2
3
4
5
6
7
8
	if(num>0)
	{
	pos++;
	}
	else if(num<0)
	{
	neg++;
	}


Also removed the cout and cin that was outside of the loop. Thank you for helping me figure this one out to everyone.
Last edited on
Topic archived. No new replies allowed.