# Write a loop that reads positive integers from standard input.....

• 09-25-2012
Gundown64
Write a loop that reads positive integers from standard input.....
Hey guys, very new to programming and need some help with a homework problem.

Quote:

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. Declare any variables that are needed.
Note a few things:
- This does not need to be a complete program, just what is asked above.
- This need to be a do-while loop.
- The spaces between the numbers is important, but I don't know how to get spaces.

Here is what I have so far:
Code:

int num, sum=0;int sumeven=0;
int numeven=0;
int totalnum=0;
do
{
cin >> num;
if (num % 2 == 0 && num >= 0)
{
sumeven = sumeven + num;
numeven++;
}
sum = sum + num;
totalnum++;
}
while (num>0);
cout<< sum, sumeven, numeven, totalnum;

Right now, the problem is the program is simply adding up ALL the numbers, not the odd, evens, etc.

Again, I am very new to this so go easy on me :D.
• 09-25-2012
qny
Wrong board? I think you want the C++ Programming board.
• 09-25-2012
Gundown64
Quote:

Originally Posted by qny
Wrong board? I think you want the C++ Programming board.

No, this is a C programming class.
• 09-25-2012
qny
Quote:

Originally Posted by Gundown64
No, this is a C programming class.

Well, then.
In C there is no "cout" or "cin"; the "<<" token has a very different meaning than in the usage in your code.

Anyway, your program appears to be almost done.
You need to introduce the variables sumodd and numodd and an else part for the if.
• 09-25-2012
whiteflags
I see two problems. In the loop that does the counting and adding you have to use an if-else because if you don't you might add a number to both totals. A number can either be even or odd, so you want to do one or the other thing.

The other problem is that the cout statement does not do what you think it does. To use cout properly always insert with operator <<.
• 09-25-2012
Gundown64
Quote:

Originally Posted by qny
Well, then.
In C there is no "cout" or "cin"; the "<<" token has a very different meaning than in the usage in your code.

Anyway, your program appears to be almost done.
You need to introduce the variables sumodd and numodd and an else part for the if.

Oh, woops, my bad. Yes, I am in C++, don't know why I thought C, but thanks for the tips.

Also, if anyone can move this thread, that would be great.
• 09-30-2012
Gundown64
Ok guys, I started working on this again. Cleaned a few things up and got my output to add spaces between the numbers. It's still not quite right, but it is getting closer. Here is my code:

Code:

int num=0;
int sum1=0;
int sum2=0;
int sumeven=0;
int sumodd=0;
int evencount=0;
int oddcount=0;

do
{
cin >> num;
if (num % 2 == 0 && num > 0)
{
evencount++;
sum1=num++;
sumeven = sum1 + sumeven;
}
else if (num > 0)
{
oddcount++;
sum2++;
sumodd = sum2 + sumodd;
}
}
while (num > 0);
cout<<sumeven;
cout<<" "<<sumodd;
cout<<" "<<evencount;
cout<<" "<<oddcount;

The program does a test input and output and here are the results.
Input: 2 4 6 8 0
My codes output: 15 0 5 0
Correct output: 20 0 4 0

So as you can see, I am close, at least, for those numbers. I need help figuring out how to get it correct. Thanks in advance!

EDIT - Ok, I realize that I need another integer here. What I have for sumeven and sumodd isn't going to work because this is not adding the correct things. I need some variable, I'll call sum, to equal the sum of the numbers entered. How do I get the integer sum to do that; to be the sum of every number that is entered?

EDIT 2 - Ok, I am getting the first part of the output to work - "prints out the sum of all the even integers read" - Still need to get the other three parts working correctly.
• 10-01-2012
Click_here
You should post your problem in the C++ forum

Code:

if (num % 2 == 0 && num > 0)
{
evencount++;
sum1=num++;
sumeven = sum1 + sumeven;
}

That should be num % 1 for even/odd
• 10-01-2012
grumpy
Quote:

Originally Posted by Click_here
That should be num % 1 for even/odd

Rubbish. The remainder on dividing any integral value by 1 is zero.
• 10-01-2012
poornaMoksha
Quote:

Originally Posted by Click_here
You should post your problem in the C++ forum

Code:

if (num % 2 == 0 && num > 0)
{
evencount++;
sum1=num++;
sumeven = sum1 + sumeven;
}

That should be num % 1 for even/odd

Whaaaat? Whats the use of having num%1 ??
• 10-01-2012
AndiPersti
Code:

if (num % 2 == 0 && num > 0)
{
evencount++;
sum1=num++;
sumeven = sum1 + sumeven;
}

Incrementing "num" after assigning it to "sum1" is unnecessary. Thus "sum1" is unnecessary too because it's the same as "num". You can reduce the body of your if-statement to
Code:

evencount++;
sumeven += num;

Code:

else if (num > 0)
{
oddcount++;
sum2++;
sumodd = sum2 + sumodd;
}

Why do you increment "sum2" and add it to "sumodd"? You should add "num" to "sumodd" as you do for even numbers.

Bye, Andreas
• 10-01-2012
Click_here
Quote:

Originally Posted by poornaMoksha
Whaaaat? Whats the use of having num%1 ??

I'm so sorry - I don't know what I was thinking typing that. I think that I was thinking of num &1.

You are right. I've been misreading things all day. I'm sorry again!!
• 10-01-2012
Gundown64
Quote:

Originally Posted by AndiPersti
Code:

if (num % 2 == 0 && num > 0)
{
evencount++;
sum1=num++;
sumeven = sum1 + sumeven;
}

Incrementing "num" after assigning it to "sum1" is unnecessary. Thus "sum1" is unnecessary too because it's the same as "num". You can reduce the body of your if-statement to
Code:

evencount++;
sumeven += num;

Code:

else if (num > 0)
{
oddcount++;
sum2++;
sumodd = sum2 + sumodd;
}

Why do you increment "sum2" and add it to "sumodd"? You should add "num" to "sumodd" as you do for even numbers.

Bye, Andreas

Thank you very much! That was the solution! The sum1 and sum2 were just something I was testing, but I knew they were wrong. Thank you! Here is the finished code:

Code:

int num=0;
int sumeven=0;
int sumodd=0;
int evencount=0;
int oddcount=0;

do
{
cin >> num;
if (num % 2 == 0 && num > 0)
{
evencount++;
sumeven += num;
}
else if (num > 0)
{
oddcount++;
sumodd += num;
}
}
while (num > 0);
cout<<sumeven;
cout<<" "<<sumodd;
cout<<" "<<evencount;
cout<<" "<<oddcount;