Hey all,
I'm fairly new to C++ and am trying to write a program that continues asking the user for a number until "0" is entered. I am supposed to count the number of positive, negative, odd, and even numbers that the user has entered once the program is terminated(0 is entered by the user). I'm having trouble with the counting portion as all I get is zero each time for all the counts. Also, I am not supposed to count the "0" that is entered when the user wants to terminate the program. Can anyone kindly point me in the right direction?? Thank you....

#include <iostream>
using namespace std;
int main()
{
	int number = 1,odd = 0,positive = 0,negative = 0;
	while (number != 0)
	{
	cout << "Please enter a number (0 to exit): ";
	cin >> number;
	}
	if ((number%2 == 0) && (number != 0))
	{
		number++;
	}
	else if (number%2 != 0) 
	{
		odd++;
	}
	else if ((number > 0) && (number != 0))
	{
		positive++;
	}
	else if ((number < 0) && (number != 0))
	{
		negative++;
	}
	
		cout << number << " even numbers" << endl;
		cout << odd << " odd numbers" << endl;
		cout << positive << " positive numbers" << endl;
		cout << negative << " negative numbers" << endl;
	
}

Well as I don't expand gigantically I use backdoors. For example when you ask for imput increment number and have a if setup to subtract one from number if what the user entered is 0.
Ex:

cin>>tacos;
number++;
if(tacos==0){
number--;
}

please make sure this if is seperare from the rest of your ifs. Or else it won't happen. Ex in a chain of else ifs once it findes a liable if to go with the imput it won't cheap the others.
EDIT: You Are asking for variable number which is also the total number of numbers the user ha entered. Change the variable that counts the total number of numbers imputed and that should work now.

You need to think about what to do here. Your loop does not extend to cover the whole requirement -- program keeps accepting and counting user's input until a "0" is entered. What you are doing now is to keep accepting until it is not "0" which results in 0 count.

Move your "}" in line 10 to line 27 and see what happens. Oh by the way, you should not need to check for number!=0 because it will never be 0 in the loop...

PS: You should not use "else-if" to check for all conditions. "else-if" creates mutual exclusive -- only true in one case and will never be true in others. There are 2 mutual exclusive conditions here, not 1. Therefore, using all "else-if" will not count the result properly.

Actually, you should have 2 if-else statements: if even increment evenCount else increment oddCount Same with positive/negative

As for exiting when 0, add one more IF to encompass everything from just after input to bottom of the loop. If != 0, do the stuff

So I THINK I'm on the right track but am stuck again. Here is what I have corrected with the program though it is still not counting correctly...

#include <iostream>
using namespace std;
int main()
{
	int number = 1,odd = 0,positive = 0,negative = 0;
	while ((number > 0) || (number < 0))
	{
	cout << "Please enter a number (0 to exit): ";
	cin >> number;
	}
	if (number%2 == 0) 
	{
		number++;
	}
	else 
	{
		odd++;
	}
	 if (number > 0) 
	{
		positive++;
	}
	else 
	{
		negative++;
	}
	
		cout << number << " even numbers" << endl;
		cout << odd << " odd numbers" << endl;
		cout << positive << " positive numbers" << endl;
		cout << negative << " negative numbers" << endl;
	 
	
}

Here are two things you should learn:

1) Always use good formatting when writing your code. Here is your code formatted nicely:

#include <iostream>
    using namespace std;
    int main()
    {
		int number = 1,odd = 0,positive = 0,negative = 0;

		while (number != 0)
			{
				cout << "Please enter a number (0 to exit): ";
				cin >> number;
			}

		if ((number%2 == 0) && (number != 0))
			number++;			
		else if (number%2 != 0)
			odd++;			
		else if ((number > 0) && (number != 0))
			positive++;			
		else if ((number < 0) && (number != 0))
			negative++;
			
     
    cout << number << " even numbers" << endl;
    cout << odd << " odd numbers" << endl;
    cout << positive << " positive numbers" << endl;
    cout << negative << " negative numbers" << endl;
     
    }

So you can clearly see that all of your else-if statements are outside of the while loop and thus only operate once during the whole program. Good formatting helps you spot errors of that sort. Always practice good formatting habits for clear code so you can see what you are doing!

2) Always throughly think through your problem BEFORE you start coding. You can't code any better than the clarity of your understanding of the problem! Because you didn't have a clear understanding of the problem, you are now faced with double trouble - is the problem in the logic of your solution or in the implementation of that logic? Start at the start and make sure your logic for the problem is correct - then just code it. Coding it is much easier when you know what you need to code.

Let's think this through here. You have to keep inputting numbers and have four variables incremented as needed to tally how many numbers were +, -, even and odd. So, let's setup four int variables to hold that data: Positive, Negative, Even and Odd. So now when ever we input a number, one or more of those four variables will be updated (incremented) to show what sort of number it is. For example, if the input was 30, then both the Positive AND Even variables would be incremented. If -6 was entered, then both the Negative and Even variables would be incremented. If 5 was entered, then just the Odd variable would be incremented.

So how do we code this? Notice that in your else-if chain there would only be one variable incremented per round, because once one variable was incremented then all of the rest of the else-if statements were skipped. An easy way to do this is to have four separate if statements: if(input == even) then ++even, if(input == odd) then ++odd, and the same for + or -. That way, if we run through four separate if statements we can bring each of the four variables to be compared to the input to see if it needs to be incremented or not.

Now that we have a simple solution, we can go from there and see how we can maybe improve it. Instead of having four separate if statements, we could have just two separate if-else statements, one for even-odd and one for + or -. Often times, esp. with complex problems, it is good to use a simple although clumbersome solution that works and then go to the next step of reducing it to "lowest terms".

One other thing: you have the problem of entering zero to end the program, but you don't want this zero to be figured in as regular input that will effect the four variables. The solution would be to test for zero as the first thing in the while loop - before you ever get to the two if-else statements, and if zero then terminate the program right then and there, after doing all of your cout statements. That way, when the program gets to the if-else statements there is no way the input could be zero, or else the program would have terminated. So you don't have to test for zero, like &&(input !=0) in each if statement.

So in summary, write well formatted code so you can see what you are doing. Make sure you understand the problem before you try to solve it. And start with any simple solution that will work, then improve on it from there.

Here are two things you should learn:

1) Always use good formatting when writing your code. Here is your code formatted nicely:

Actually, his code is formatted better than yours. The only bad thing about his formatting is inside the initial while statement (not indented) and the last cout statements (indented too far). Your indentation is much too deep on every single line of code except the final cout statements.

So how do we code this? Notice that in your else-if chain there would only be one variable incremented per round, because once one variable was incremented then all of the rest of the else-if statements were skipped.

Not true. His "else-if" (properly if-else ) chain was correct. He had 2 IF 's, but your reformatted version was modified to create only 1.

The points about
1) "think through your problem"
2) "your else-if statements are outside of the while loop"
3) "you have the problem of entering zero to end the program"
are correct.

Great! You are on the right track! But you still need to extend your loop scope to cover your if-else statement... Also, the "else" may be changed to "else if" instead if you do not want to check for "0" again. I am sorry for misled you in my earlier post about no need to check for it. You actually need it as WaltP said, but you could easily combine it in the "else if" condition.

**Edited**
Just rethink again and actually you do not need to check for "0" in your if-else statement if you ask for a number before you start your while-loop. Then you ask for a number again at the bottom of the while-loop content. This way, the while-loop is checking for "0" instead of your if-else statement.

Not true. His "else-if" (properly if-else ) chain was correct. He had 2 IF 's, but your reformatted version was modified to create only 1.

Actually, my reply was in regards to the first post made by the poster, and he had made his updated post 30 minuites before I made mine which I didn't realise. I should have included a quotation from his original post in my reply.

So yes, his if-else statements are correct now. Sorry for the confusion.

So I changed my code around a little and I think i'm almost there but i'm still getting the wrong counts for some of the categories.

#include <iostream>
using namespace std;
int main ()
{
	int num = 1, even = 0, odd = 0, positive = 0, negative = 0;

	while (num != 0)
	{
		cout << "Please enter a number (0 to exit): ";
        cin >> num;
		if (num %2 == 0)
			even++;
		else if (num %2 != 0)
			odd++;
	    if (num > 0)
			positive++;
		else if (num < 0)
			negative++;
	}
	cout << even << " even numbers"<< endl;
	cout << odd << "  odd numbers"<< endl;
	cout << positive << " positive numbers"<< endl;
	cout << negative << " negative numbers"<< endl;
}

So I changed my code around a little and I think i'm almost there but i'm still getting the wrong counts for some of the categories.

When asking for help, details are needed. What's wrong with the counts?


See the comments:

#include <iostream>
using namespace std;
int main ()
{
	int num = 1, even = 0, odd = 0, positive = 0, negative = 0;

	while (num != 0)
	{
		cout << "Please enter a number (0 to exit): ";
		cin >> num;
		//// What if you entered zero? You still process it...

		if (num %2 == 0)
			even++;
		else if (num %2 != 0)  // Why the IF? the ELSE is guaranteed to be odd
			odd++;
		if (num > 0)
			positive++;
		else if (num < 0)  // Same here, if you don't process the 0
			negative++;
	}
	cout << even << " even numbers"<< endl;
	cout << odd << "  odd numbers"<< endl;
	cout << positive << " positive numbers"<< endl;
	cout << negative << " negative numbers"<< endl;
}

And don't mix TABs and SPACEs with your indents. Change all tabs to 4 spaces in your IDE so your indenting works better.

So I changed my code around a little and I think i'm almost there but i'm still getting the wrong counts for some of the categories.

Let me give you a clue to the scene of the crime (taken from my first reply):

<< One other thing: you have the problem of entering zero to end the program, but you don't want this zero to be figured in as regular input that will effect the four variables. >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.