simple while loop

I am trying to write a program that takes a list of integers (for example: 7, 8, 10, 15, 26, 28, 9) and a) add them together, b) add the even ones together, and c) add the odd ones together. I started with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main ()
{
int count = 7;
int number = 0, total = 0, totalOdd = 0, totalEven = 0;

while (count < 0)
{
	cin >> number;
	total += number;
	if (number %2 == 0)
		totalEven += number;
	else totalOdd += number;
}
}
cout << total << totalOdd << totalEven << endl;

, but I get error messages like "missing ';' before <<" in the cout statement,
and "missing type specifier -int assumed" also in line 19.
I know I'm missing other things too, but what? Do I have to give the list of integers somewhere? Please help. Thank you!
Your cout is not inside main(). It needs to be.

Otherwise the only other problem I see at first glance is while(count<0) isn't
going to do much when count starts > 0.
Your output will be smushed together like this too
124324

not 1 32 45

Just add a tab or something in there
Well, the good thing about compilers is that they tell you what they think is wrong.

In this case, a ";" is missing before "<<"
The "<<" is in line 19, so the ";" must be missing there - but it is not missing there. So it must be missing "before" that.
And well, line 17 should be "};" <-- the missing ";".
And Line 18 should be after line 19.

int main
I have made slight modifications to the code
the code that u have written seems like there is some error
in the logic

This programm takes 2 inputs

1. Number of elements
2. Elements to be added up


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
26
#include <iostream>

using namespace std;

int main ()
{
int count;
int number = 0, total = 0, totalOdd = 0, totalEven = 0;

cout<<" Enter the Number of elements "<<endl;
cin>>count;

while (count != 0)
{
        --count;
        cout<<"Enter the Number :"<<std::endl;
        cin >> number;
        total += number;
        if (number %2 == 0)
                totalEven += number;
        else totalOdd += number;

}
cout << total <<"\t"<< totalOdd <<"\t" << totalEven << endl;

}


can u compile the program and let me know if it works fine
Last edited on
Topic archived. No new replies allowed.