943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4963
  • C++ RSS
Oct 6th, 2008
0

Trouble with loops - Calculate sum of even and odd integers

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num=0, even=0, odd=0, i = 0;
  9. char stop = 'a';
  10.  
  11. cout << "Enter a list of integers followed by the '$' sign: " << endl;
  12. cin >> num;
  13. stop = num;
  14.  
  15. while(stop != '$')
  16. {
  17. if(num % 2 == 0) //If the number is even
  18. even = even + num; //Add it to the current sum of even integers
  19. else if(num % 2 != 0) //if the number is odd
  20. odd = odd + num; //Add it to the current sum of odd integers
  21. else
  22. {
  23. i = 1; //Otherwise, make i equal to 1
  24. }
  25.  
  26. cin >> num;
  27. stop = num;
  28. }
  29.  
  30. if(i == 1)
  31. cout << "Please enter only integers" << endl;
  32. else if(i == 0)
  33. {
  34. cout << "The sum of the even integers is: " << even << endl;
  35. cout << "The sum of the odd integers is: " << odd << endl;
  36. }
  37.  
  38. return 0;
  39. }

Thanks for the help
Last edited by ShadowOfBlood; Oct 6th, 2008 at 3:43 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

Look at line 13

C++ Syntax (Toggle Plain Text)
  1. 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)
  1. while(num != -1)
Last edited by chococrack; Oct 6th, 2008 at 3:54 am.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

The question states: Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers.

I'm assuming it means positive and negative integers. Any ideas?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

Nvm, I was going about it wrong. I figured it out. Here's what I have:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (ex. - 12345)" << endl;
  11. cin >> num;
  12.  
  13. while (num > 0)
  14. {
  15. dig = num % 10;
  16. num = num / 10;
  17.  
  18. if(dig % 2 == 0)
  19. even = even + dig;
  20. else
  21. odd = odd + dig;
  22. }
  23.  
  24. cout << "The sum of the even integers is: " << even << endl;
  25. cout << "The sum of the odd integers is: " << odd << endl;
  26.  
  27. return 0;
  28. }

Thanks for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

Dont You Think it is giving you the wrong answer?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

It was giving me right answers when I checked it O.o

Is something wrong with it?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

Nvm, I was going about it wrong. I figured it out. Here's what I have:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (ex. - 12345)" << endl;
  11. cin >> num;
  12.  
  13. while (num > 0)
  14. {
  15. dig = num % 10;
  16. num = num / 10;
  17.  
  18. if(dig % 2 == 0)
  19. even = even + dig;
  20. else
  21. odd = odd + dig;
  22. }
  23.  
  24. cout << "The sum of the even integers is: " << even << endl;
  25. cout << "The sum of the odd integers is: " << odd << endl;
  26.  
  27. return 0;
  28. }

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.
Reputation Points: 57
Solved Threads: 2
Junior Poster in Training
bhoot_jb is offline Offline
89 posts
since Mar 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (-7777 to terminate)" << endl;
  11. cin >> num;
  12.  
  13. while (num != -7777) // off the wall check value
  14. {
  15.  
  16. // dig = num % 10;
  17. // num = num / 10; <--- what are these for?
  18.  
  19.  
  20.  
  21. if(dig % 2 == 0) // if the number is even
  22. even += num; // add it to the previous even sum
  23. else
  24. odd += num; // otherwise add it to the odd sum
  25.  
  26. cout << "Enter next number: ";
  27. cin >> num;
  28. }
  29.  
  30. cout << "The sum of the even integers is: " << even << endl;
  31. cout << "The sum of the odd integers is: " << odd << endl;
  32.  
  33. return 0;
  34. }
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

I haven't learned arrays yet :\

And what if the user wants to input -7777 as part of the list?

I dunno what I should do with this program :\
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 6th, 2008
0

Re: Trouble with loops - Calculate sum of even and odd integers

K, class has arrived. Thanks for the help =)

I decided to use the example with the -7777 terminating value =)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Class Link-List help!?
Next Thread in C++ Forum Timeline: Comparision Between Tarbo C++, Visual C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC