For class I need to write three programs; one using 'do-while', one using 'for', and another using just 'while'. I figured out the 'for' part of the assignment thanks to the book, but I'm a little foggy on using just 'while'. Here's what I have so far, forgive the crude nature of the code:
I need to enter a series of integers, so limiting the number of integers added is part of the code as well. That was easy enough using 'for', but its a bit cloudy using 'while'.
The code compiles just fine, but the program crashes when run. I have a feeling I'm missing something simple. Can someone give me a little push over the edge, so to speak?
The first cin you have on line 12 before entering the loop is completely ignored. It's not added to sumodd or sumeven, and it doesn't count toward the total quantity of numbers entered. Simply remove that line.
That accounts for one extra number- the other is because you're starting the counter at 0 and looping as long as it's less than or equal to ten- in other words, it's looping 11 times. You have two choices.
change "counter = 0;" to "counter = 1;"
or
change "while (counter <= N)" to "while (counter < N)"
I prefer the latter, although it's not at all a big deal.
You probably have the same book that I do! Though I hear its a popular program for a beginner's course.
I took out the first cin and changed the <= to < last night shortly after my last post, and everything seems to work fine. Space knows I'll be back when I'm writing the 'do-while' code.
Thanks for the help!