| | |
Trouble with loops - Calculate sum of even and odd integers
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
As a homework assignment, I was asked to write a program in C++ that calculates the sum of the even and odd integers in a list of integers given by the user. I think I'm on the right track, but I can't seem to get my loop right. Here's what I have so far:
Thanks for the help
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int num=0, even=0, odd=0, i = 0; char stop = 'a'; cout << "Enter a list of integers followed by the '$' sign: " << endl; cin >> num; stop = num; while(stop != '$') { if(num % 2 == 0) //If the number is even even = even + num; //Add it to the current sum of even integers else if(num % 2 != 0) //if the number is odd odd = odd + num; //Add it to the current sum of odd integers else { i = 1; //Otherwise, make i equal to 1 } cin >> num; stop = num; } if(i == 1) cout << "Please enter only integers" << endl; else if(i == 0) { cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; } return 0; }
Thanks for the help
Last edited by ShadowOfBlood; Oct 6th, 2008 at 3:43 am.
Look at line 13
Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."
Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.
Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.
C++ Syntax (Toggle Plain Text)
stop = num;
Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."
Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.
Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.
C++ Syntax (Toggle Plain Text)
while(num != -1)
Last edited by chococrack; Oct 6th, 2008 at 3:54 am.
I would love to change the world, but they won't give me the source code
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
Nvm, I was going about it wrong. I figured it out. Here's what I have:
Thanks for the help
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int even = 0, odd = 0, dig, num; cout << "Please enter a list of numbers (ex. - 12345)" << endl; cin >> num; while (num > 0) { dig = num % 10; num = num / 10; if(dig % 2 == 0) even = even + dig; else odd = odd + dig; } cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; return 0; }
Thanks for the help
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
•
•
•
•
Nvm, I was going about it wrong. I figured it out. Here's what I have:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int even = 0, odd = 0, dig, num; cout << "Please enter a list of numbers (ex. - 12345)" << endl; cin >> num; while (num > 0) { dig = num % 10; num = num / 10; if(dig % 2 == 0) even = even + dig; else odd = odd + dig; } cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; return 0; }
Thanks for the help
what is quite confusing is the 'num' integer.
You said that you are taking input of a list of integers while you have just asked for a single integer.
I assume that you have taken the input of "list of integers" in a single integer 'num' itself, i.e., you have considered the value of 'num' as a list of integers (digit by digit)

if this is what you have assumed, i think this is a wrong approach. You would have rather used an array.

Think upon it.
Bhoot
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int even = 0, odd = 0, dig, num; cout << "Please enter a list of numbers (-7777 to terminate)" << endl; cin >> num; while (num != -7777) // off the wall check value { // dig = num % 10; // num = num / 10; <--- what are these for? if(dig % 2 == 0) // if the number is even even += num; // add it to the previous even sum else odd += num; // otherwise add it to the odd sum cout << "Enter next number: "; cin >> num; } cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; return 0; }
I would love to change the world, but they won't give me the source code
![]() |
Other Threads in the C++ Forum
- Previous Thread: Class Link-List help!?
- Next Thread: Comparision Between Tarbo C++, Visual C++
Views: 2350 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





