Don't understand this program

I'm using "C++ primer 4th edition" to learn C++ with, and it had this program:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
	int sum = 0, value;
	// read till end-of-file, calculating a running total of all values read
	while (std::cin >> value)
	{
		sum += value; // equivalent to sum = sum + value
	}
	std::cout << "Sum is: " << sum << std::endl;
	return 0;
}


It said this would read an unknown number of inputs and sum all the inputed numbers up. Like if you put in the numbers "1 2 3", it would sum those up and give an output of "6".

However, when I compiled and ran this program it wouldn't do anything, I entered the numbers "1 2 3" and pressed enter(return), but it just made a newline, and nothing else.

I'm assuming this is because it doesn't know to go on to the "while" part. But the book says it should work just fine. So what is wrong with it?
Last edited on
This is a simple problem.

The small error that you are doing is out making an endless loop.

Try doing this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
	int sum = 0, value;
	
	while (std::cin >> value)
	{
		sum += value; 
                        std::cout << "Sum is: " << sum << std::endl;
	}
	
	return 0;
}


This is what u are actually trying to do... I believe.

The problem with your original code is that you are setting the program into an endless loop. Between the "while part".

Generally, a while structure goes like this:

1
2
3
4
While (condition) 
{
Code executed
}


Because in your condition, it is simply telling to go get a value (cin >> value), it will always continue with the "while" part and enver go onto line 11 (std::cout << "Sum is: " << sum << std::endl;).

Hope this helps, if u have any more questions, please ask, however, I must warn u, I'm not a C++ expert, but a fellow beginner just like yourself.
Last edited on
OK, that worked, thanks for the fix. The book didn't have the block symbols after the "While" line, it was all in the same block. Like, everything was in the main() function block, no others, but I always add the block symbols to help make the code less confusing (to me).

So, your method work. But I looked at it and thought: "How is this going to exit the loop", so I added a cout statement saying when the "While" block was done. If the cout statement popped up, I would know it had exited the "While" part of the program. The cout statement never popped up, so what I am assuming that this program isn't really meant to end, but to just keep checking for input until you close out of the program. Is that what it should do?


EDIT: OK, I reread your post, and it looks like you answered my post already, by saying
because in your condition, it is simply telling to go get a value (cin >> value), it will always continue with the "while" part and never go onto line 11 (std::cout << "Sum is: " << sum << std::endl;).
. I just didn't catch it the first time.
Last edited on
Ok just glad I could help!

Just making sure, I've ANSWERED your question... right?

Now if u can, plz answer my other question on:

http://cplusplus.com/forum/beginner/21361/
Last edited on
Topic archived. No new replies allowed.