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.
Thanks in advance!