Not printing correctly

It's not accepting input?

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

int main()
{

	int sum = 0, value = 0;
	while (std::cin >> value)
	sum += value;
	std::cout << "Sum is: " << sum << std::endl;
	return 0;
}
It will accept integers as input; and stop accepting input if when it encounters a non-integer.
http://coliru.stacked-crooked.com/a/671597007c196b40
Hello Pepeforever,

What is not accepting input? I do not follow what you mean.

Other than the blank lines and proper indenting the only change is in the while condition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{

	int sum = 0, value = 0;
	
	while (std::cout << " Enter a number: " && std::cin >> value)
		sum += value;
	
	std::cout << "Sum is: " << sum << std::endl;
	
	return 0;
}


Which produced this output in the shell program here.

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: a
Sum is: 10



Andy
I put in integer values but when I press enter it goes to the next line on the console
Keep entering another integer on that next line;
when all the numbers have been entered, enter something invalid, like 'abcd'
Hello Pepeforever,


I put in integer values but when I press enter it goes to the next line on the console


Without a prompt what did you expect to happen?

Andy
@JLBorges

Why did it work when I tried what you said?
> Why did it work when I tried what you said?

Formatted input (std::cin >> value) skips leading whitespace characters in the input buffer. It skips spaces, newlines etc. till it encounters the next non-whitespace input character and then starts reading from there onwards.
Topic archived. No new replies allowed.