You are here:

C++/Program that reads a set of integers, and then finds and prints the sum of the even and odd integers

Advertisement


Question
This is what I have so far need to write with the while loop and cannot figure it out thanks

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int num=0, even=0, odd=0, notInteger=0;
char stop = 'a';

cout << "Enter the list of integers that you want to get added followed by '#' sign " << endl;
cin >> num;
stop = num;

while(stop != '#')
{
if(num % 2 == 0) // Checking for the even number.
even = even + num; // If even then increment the value of even by the number itself
else if(num % 2 != 0) // Checking for the odd number.
odd = odd + num; // If odd then increment the value of odd by the number itself
else
{
notInteger = 1; // This condition is used for all the other things, like as if user typed some alphabets instead of number
// then this condition will be used to prompt the user to type only integers greater then 0.
}

cin >> num;
stop = num;
}

if(notInteger == 1)
cout << "Please enter only integers" << endl;
else if(notInteger == 0)
{
cout << "The sum of the even integers is: " << even << endl;
cout << "The sum of the odd integers is: " << odd << endl;
}

return 0;
}


Answer
Dear Jerry:

See the comments I have made in your code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int num=0, even=0, odd=0, notInteger=0;
int stop ; // Stop must be an integer because its value comes from an integer, otherwise, you may lose data because integer is wider than char.  If you really wanted to do this you would cast it to show others you really meant to do it.

//do
//{ // etc. Add the integers that are inputted to a container like an array.  You must do this whether you input and parse a string or input the integers one at a time in a loop.
cout << "Enter the list of 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 ) // Looks like you want to enter a list of integers.  But the way you coded it, the only thing that will work is to enter one integer.  I modified your program to work for entering one integer (maximum).  If you want to enter a list of integers (say separated by coma's or spaces) you will have to enter a character string and parse the character string.  Or if you want to enter a list of integers one integer at a time, put the three input lines above in a do-while loop.
{
if (num % 2 == 0) // Checking for the even number.
{ // This probably worked before, but it is easier for you and those who read your code to use brackets.  Also you will less likely make a mistake e.g. copying and pasting this code.
  even = even + num; // If even then increment the value of even by the number itself
}
else if(num % 2 == 1) // Checking for the odd number.
odd = odd + num; // If odd then increment the value of odd by the number itself
else // This does not work because the result of ( num %2 ) will always be equal to 1 or 0 even if num was a character. In any event, num is declared as an int anyway.
{     // In other words the representation of a char in C++ is a byte which is an integer ( a one byte integer ).
notInteger = 1; // This condition is used for all the other things, like as if user typed some alphabets instead of number
// then this condition will be used to prompt the user to type only integers greater then 0.
cout << "Please enter only integers" << endl; // This does not work (will never execute), but if it did, it would have to be coded this way.
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;
}

return 0;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.