while loop keeps asking for input after EOF

This is driving me nuts! This is an example from C++ Primer on while loops shortened for simplicity:

int main() {

int value = 0;

while(cin >> value) cout << value;

return 0;
}

When I compile and run the above code the program keeps asking for input after I input nothing but pressing ENTER no matter how many times. The only way I can get it to stop asking for input is to input something other than an int such as a char or string. Program executes as intended after that. I have googled this issue and read all seemingly relevant results and nothing seems to pertain to my exact problem. I think it may have something to do with my computer's own settings or something and am baffled as to what it may be. Please can anyone here help? Thank you if so.

Well the input stream never has an end of file. Now if you have a file you can do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream infile;
	infile.open("infile.txt");
	
	string data;
	
	while(!infile.eof())
	{
		getline(infile, data);
		cout << data; 
		cout << endl;
	}
	
	infile.close();
	return 0;
}

Testfile wrote:

Some Data
Some More Data
Yet again more data

OUTPUT

Some Data
Some More Data
Yet again more data


Just a quick example.
Last edited on by closed account z6A9GNh0
Sorry I guess eof was poor term to use, invalid input would maybe be better. Regarldess in the example I provided shouldn't the while loop cease when just pressing Enter without typing a number and not require I input something other than an int to cease the loop? Thank you and sorry for the confusion.
No it will never end because as far as I know the stream is never empty so while(cin) is the same as saying while(true), an infinite loop.
Well that is strange C++primer uses the example of while(cin >> value) in multiple examples of how to loop through an unknown amount of type int numbers input by a user. Even when I searched for what I was doing wrong online I saw more examples of it being done this same way. So how is a while loop used when reading a beforehand unknown number of user inputs? Thank you again for your help.
Try entering a char.
@naraku Yes entering a char gets the while loop to terminate but that is what I stated in my first post. My problem is in the book and elsewhere online I have seen while(cin >> value) used in multiple examples to read an unkown amount of user inputs and the loop is exited when the user just hits Enter with no more numbers being input. I just can't figure out for the life of me why I can't get this simple example to work on my machine.
May be a typo in the book, stream extraction with >> stops on whitespace so enter keypress is essentially skipped. The condition in the while loop only fails when the extraction from the stream fails.
Going to approach this in a different way. Below is the exact example provided in C++ Primer. Please tell me if syntax is correct and if so exactly how the program should behave when executed. I can't believe I have already been stopped by such a seemingly simple example.

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

int main() {
	int sum = 0, int val = 0;
	while( std::cin >> val)
		sum += val;
	std::cout << "Sum is " << sum << std::endl;
	return 0;
}
Wow after that last post and re-reading the book one more time I think I misunderstood that the book meant for me to hit ctrl+z to halt loop if I tested that code snippet for myself. I thought the loop would end if I just hit Enter with no integer input on that line. Please confirm this for me if someone can please. I can't believe I have wasted all night on this, whoops!
Yeah, Bjarne's book has an example like that too, but you have to do CTRL+D when you are done for it to work. I always forget about that when I see these examples.
Topic archived. No new replies allowed.