![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
1 loop structure to find the sum of odd and even integersHello, I am new to this forum and to programming. I was trying to make a small program that calculates the sum of odd and even integers up to the limit provided by the user. The code is given below. The problem is to find the even sum. I am stuck at this point. Please guide me.
CPP / C++ / C Code:
Last edited by admin : 24-Oct-2009 at 05:02.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
||||
|
||||
Re: 1 loop structure to find the sum of odd and even integersThere is no standard header named conio.h...don't use it unless required by your course/instructor...and then question why you must use it.
I don't think that I'd use a while conditional; rather a for seems more suitable: CPP / C++ / C Code:
MxB |
|
#3
|
|||
|
|||
Re: 1 loop structure to find the sum of odd and even integersThank you for your guidance. You said that conion.h is not a standard header. I have used your code but the window disappears so fast that I cannot see the output. However if I use conio.h with getch(); in the end I can view my output of the program. Is it wrong to use it and write the code as you did in the example?
|
|
#4
|
||||
|
||||
Re: 1 loop structure to find the sum of odd and even integersQuote:
"Wrong" is relative. There are alternatives to using non-standard libraries and headers: CPP / C++ / C Code:
MxB |
|
#5
|
|||
|
|||
Re: 1 loop structure to find the sum of odd and even integersYou can also pause with this without having to use a variable:
CPP / C++ / C Code:
Last edited by Howard_L : 26-Oct-2009 at 21:47.
|
|
#6
|
||||
|
||||
Re: 1 loop structure to find the sum of odd and even integersQuote:
I'm not sure that this is true in all cases on all platforms. For example, the following change: CPP / C++ / C Code:
...just ran without pausing, as shown below: Code:
...on Mac OS X 10.5.8 using GNU GCC. MxB |
|
#7
|
|||
|
|||
Re: 1 loop structure to find the sum of odd and even integersQuote:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: 1 loop structure to find the sum of odd and even integersAh yes, the leftover '\n' and extra characters typed. Good point M.
And nice explaination Kimmo. Well I guess you could "flush" stdin after user input like you should do any time you want to get meaningful subsequent user input. CPP / C++ / C Code:
My platform is pine flooring on 2x4 joists about 12" above dirt... (gcc / fc6-linux) |
|
#9
|
|||
|
|||
Re: 1 loop structure to find the sum of odd and even integersCPP / C++ / C Code:
Perhaps, but then you need to be sure every time there actually is a newline. An alternative is to use getline and stringstreams. CPP / C++ / C Code:
Meaningless output: Code:
|
|
#10
|
|||
|
|||
Re: 1 loop structure to find the sum of odd and even integersWow, that's an interesting way of handling user input.
You mean there is a way for user to enter data without pressing the Enter key? Doesn't this always leave at least the '\n' like scanf()? CPP / C++ / C Code:
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The