I need to finish this.

**Write a program that reads a set of 10 integers, and then finds and prints the sum of the even and odd integers.**
I don't got a clue what's wrong with it.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int num=0, even=10, odd=0, notInteger=0;
	int stop ;
	do
	{
	cout << "Enter the list of 10 integers that you want to get added followed by '#' sign " << endl;
	cin >> num;
	stop = num;
	}while ( (char) stop  != '#' ) ;
	for ( int num = 0 ; num < stop ; ++ num )
	{
		if (num % 2 == 0)
		{
	}
		else if(num % 2 == 1)
		odd = odd + num;
		{
	notInteger = 1;
		cout << "Please enter only integers" << endl;
		cin >> num; 
	stop = num;
	break ;
	}

	}

	//if(notInteger == 1)
	if(notInteger == 0)
	{
		cout << "The sum of the even integers is: " << even << endl;
		cout << "The sum of the odd integers is: " << odd << endl;
	}
		system("pause");
		return 0;
Please don't double post, you already have a topic about this.
First of all there are several syntactical flaws with the code you've written. The body of function main doesn't have a closing brace and when I ran the code the compiler says that _TCHAR* has not been declared so you are much better of just keeping it simple. Just use
int main()
for main's function header instead.

Also the variables for keeping track of the even and the odd integers should be inside the do..while loop. And you should consider prompting the user for one integer at a time. This way the integer is checked against the conditions (whether even or odd) and then stored in the appropriate variable before the user is asked for another integer. If you ask the user for a list of the integers all at once and the direct the computer to only one variable for storing them, the computer will store the first variable it finds and leave the rest.

You also have a problem with lines 16 - 29. Not only is it heavily flawed in its logic but the for loop for adding the sum of the even integers and the sum of the odd integers does not depend on what the user typed in but on only the integers from 1 to 10 and will use these regardless of what the user typed in.

The mechanism you have put in place to check whether the input from the user is an integer or not is not clear in its logic. I don't see how notInteger is initialized according to what the user has typed in and even if I did there it is not inside any selection statement like "if" or "if...else" that would make it useful.

Finally even if you did all of the above your program would be stuck in an infinite loop once the user typed in "#". This is because the do..while loop does not cater for anything typed in that isn't an integer. I noticed that you tried using a cast operator to try converting the type from int to char but this doesn't work because your program goes awry at input so once something other than an integer is typed it goes into an infinite loop. I suggest that you put the mechanism for checking whether what is typed is an integer or not immediately after input before storing it in a variable to be used for the summing. If you can't do this right away and you want to see your program work you can use '0' instead of '#' to signify end of input and break out of the loop. If so your code should look something like this:

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
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
	int num=0;
	int even=0, odd=0;
	cout << "Enter the integers that you want to get added one by one\nPlease make sure that you enter only integers " << endl;
	cin >> num;
	while ( num  != 0 ) 
	{
	  (num % 2 == 0)
	{
		even = even + num;
	}
	else if(num % 2 == 1)
		{
			odd = odd + num;
		}
   
	cout << "Enter the next integer but enter 0 if you are done " <<endl;
	cin >> num;
	}
	
		
	
		cout << "The sum of the even integers is: " << even << endl;
		cout << "The sum of the odd integers is: " << odd << endl;
		system("pause");
		return 0;
}


It's not perfect but it works. The output is something like this:

Enter the integers that you want to get added one by one
Please make sure that you enter only integers
1
Enter the next Integer but enter 0 if you are done
2
Enter the next Integer but enter 0 if you are done
3
Enter the next Integer but enter 0 if you are done
4
Enter the next Integer but enter 0 if you are done
5
Enter the next Integer but enter 0 if you are done
6
Enter the next Integer but enter 0 if you are done
7
Enter the next Integer but enter 0 if you are done
8
Enter the next Integer but enter 0 if you are done
9
Enter the next Integer but enter 0 if you are done
0
The sum of the even integers is: 30
The sum of the odd integers is:25
Press any key to continue...


And might I suggest getting a decent IDE (like code::blocks 10 which is free) so that you don't have to use system("pause"). Plus depending on the compiler you might have to use the header file <cstdlib> before using system("pause")

To be honest it doesn't look like you put a lot of thought into writing your program and it's as if you wrote the code haphazardly. So I suggest you think your code through.
Ok, Thanks for the help and the heads up. I'm truly sorry about the double post, but it that I thought people will not post on the other post I made. :(
Btw, by the help and pointers of you guys, I made the program perfectly; Thanks y'all. :)
Topic archived. No new replies allowed.