I'm doing exercise 1.16 (p. 17) in C++ primer. The exercise is to get a set of numbers from user input (std::cin)
and add them and output (std::cout)
. My loop won't terminate with std::cin >> input
as the condition. Once it runs out of numbers to add it reads from the keyboard again.
I'm a little familiar with C and in that language I believe we can do something like while (input != \n)
, but I don't know what the character at the end of the buffer would be for std::cin
. What is the terminating/last value in std::cin
and why is it not working in my condition?
#include<iostream>
int main()
{
int x = 0;
int sum = 0;
std::cout << "Enter a set of integers: " << std::endl;
while (std::cin >> x)
sum += x;
std::cout << "The sum of those integers is " << sum << std::endl;
std::cin.get();
std::cin.get();
return 0;
}