what's wrong with the following codes?

Apr 28, 2009 at 12:20am
i typed a small fragment of codes from C++ primer p.19, but can't get the expected result. what's wrong with these codes, or my compiler (Dev-C++ 4.9.9.2)?

----
#include <iostream>
int main()
{
int sum = 0, value;
while (std::cin >> value)
sum += value;
std::cout << "the sum is: " << sum << std::endl;
return 0;
}
----
i input: 3, space, 4, space, 5, space, 6, space, then, enter, but got nothing.

Thanks for any reply.

cplusplusnew
Apr 28, 2009 at 12:32am
It is because if you type "3 4 5 6", the while loop is STILL running(notice the loop requires a number to run,and your input "6" clearly does not terminate the loop), so the program assumes you still have something to input. You must stop the loop by inputing some non-numbers, such as 1 space,2 space,3 space and "e"(or any alphabet or else)
can i help you?
Apr 28, 2009 at 12:48am
@Beginner101,
Thanks for your reply.
still doesn't work. value is an int type, and when i input a non-number following the preceding input, the program ends and the console is shut down immediately.

By the book of <<C++ primer >>, it reads,
----
If we give this program the input
3 4 5 6
then our output will be
the sum is: 18
----

any tips?
thanks again.

cplusplusnew
Apr 28, 2009 at 12:54am
system("pause");

i think you are talking about this stuff
copy and paste this sentence at the end of the program(i.e. after return 0 and before the closing curly bracket)
Apr 28, 2009 at 1:09am
Thank you, beginner101.
inserting system("pause") before return 0 works.
thank you!

cplusplusnew
Topic archived. No new replies allowed.