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>
usingnamespace 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!
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.
#include <iostream>
usingnamespace 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