I need to keep track of odd and even number which will be accepted from the user 10 time after the user entering them I have to print something like odd number = "The number of odd numbers from what she/he is inputed"

and even numbers = "The number of even numbers she has inputed"

here is my code but its not working hey

void main()
{
	int Num;
	cout << "EVEN - ODD " << endl;
	cout << "Enter a set of numbers between 1 and 100" << endl; 
	int x;
	int oddCount, evenCount;
	int y = 0;
    for(x = 1; x <= 10; x++)
	{
		cin >> Num;
			if((Num % 2) == 0)
			{
				oddCount++;
				cout << "Total Even numbers = " << x << endl;
			}
			else
			{
				evenCount;
				cout << "Total Odd numbers = " << y << endl;
			}
		
	}
	
}

Recommended Answers

All 10 Replies

Member Avatar for jencas

1. evenCount and oddCount are not initialized with 0
2. evenCount is not incremented
3. you should cout oddCount and evenCount and not uninitialized x and y which is initialzed with 0 and never changed

I have tried that but its not working

I have tried that but its not working

You could help us out by indicating what's wrong. :icon_mrgreen:
You're incrementing oddCount for even numbers and vice versa.
I imagine you only want the totals output after all input has been read, so move the cout statements (with changes recommended by jencas) to after the for loop.

What is wrong is that it is not keeping track of odd numbers or even numbers the users enters but instead it just print 1 for odd numbers only,

int Num;
	cout << "EVEN - ODD " << endl;
	cout << "Enter a set of numbers between 1 and 100" << endl; 
	int x;
	int oddCount = 0, evenCount = 0;
	int y = 0;
    for(x = 1; x <= 10; x++)
	{
		cin >> Num;
	}
	if((Num % 2) == 0)
	{
		oddCount++;
		cout << "Total Even numbers = " << oddCount << endl;
	}
	else
	{
		evenCount++;
		cout << "Total Odd numbers = " << evenCount << endl;
	}

input
12
13
14
1
3
8
78

Odd numbers = 4
Even numbers = 3

but it only print

odd numbers = 1

What is wrong is that it is not keeping track of odd numbers or even numbers the users enters but instead it just print 1 for odd numbers only,

Yes, I missed it because your alignment is a bit off. The for loop should extend to the end of the counter incrementing. At the moment it only inputs to Num and is constantly overwriting it. So extend the for loop to the end, then move the cout statements after it and do the other fixes recommended in earlier posts.[code=c++]
Yes, I missed it because your alignment is a bit off.
The for loop should extend to the end of the counter incrementing. At the moment it only inputs to Num and is constantly overwriting it.
So extend the for loop to the end, then move the cout statements after it and do the other fixes recommended in earlier posts.

If I do that, I only accept one number frm the user and it prints out this statement "Odd Numbers = 1"

If I do that, I only accept one number frm the user and it prints out this statement "Odd Numbers = 1"

Ok, I probably shouldn't be doing it for you, but to make it clear, you want something like this:

for(x = 1; x <= 10; x++)
{
    cin >> Num;
    if((Num % 2) == 0)
    {
        evenCount++;
    }
    else
    {
        oddCount++;
    }
}
cout << "Total Even numbers = " << evenCount << endl;
cout << "Total Odd numbers = " << oddCount << endl;

Well well,
Look what are doing.
You should extend the for loop till last.
put the line 14 and 19 out of the loop.
Got it?
So here is a brief algo:
Repeat 10 times
{
input a number
If it is odd, increment oddCount. If it is even increment evenCount.
}
cout the oddCount and evenCount

Ok, I probably shouldn't be doing it for you, but to make it clear, you want something like this:

for(x = 1; x <= 10; x++)
{
    cin >> Num;
    if((Num % 2) == 0)
    {
        evenCount++;
    }
    else
    {
        oddCount++;
    }
}
cout << "Total Even numbers = " << evenCount << endl;
cout << "Total Odd numbers = " << oddCount << endl;

But U actually did... Thanx a lot man, it wasnt difficult now was it

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.