![]() |
| ||
| Trouble with loops - Calculate sum of even and odd integers 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: #include <iostream> Thanks for the help :) |
| ||
| Re: Trouble with loops - Calculate sum of even and odd integers Look at line 13 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. while(num != -1) |
| ||
| 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? |
| ||
| 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: #include <iostream> Thanks for the help :) |
| ||
| Re: Trouble with loops - Calculate sum of even and odd integers Dont You Think it is giving you the wrong answer? |
| ||
| 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? |
| ||
| Re: Trouble with loops - Calculate sum of even and odd integers Quote:
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. |
| ||
| Re: Trouble with loops - Calculate sum of even and odd integers #include <iostream> |
| ||
| 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 :\ |
| ||
| 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 =) |
| All times are GMT -4. The time now is 10:57 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC