When is end-of-line set? [closed]

I faced this problem when I tried to press ctrl + z to activate the end-of-file key when working on istream object cin. My code below:

ofstream output("test.dat");
string name;
string accountNum;
double balance;
cout << "? ";
while (cin >> numAccout >> name >> balance) {
  output << numAccout << ' ' << name << ' ' << balance << endl;
  cout << "? ";

when I tried to press ctrl+z 3 times at different positions: ‘ctrl + z’, ‘100 cltr+z’, ‘100 john ctrl+z’

So the first and the last try make the program stop input but the second try is not!! @@
Can anyone help me with that, please??

try to understand when end-of-file can activate

  • 2

    ⟼Remember, it’s always important, especially when learning and asking questions on Stack Overflow, to keep your code as organized as possible. Consistent indentation helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what’s going on.


  • 5

    Ctrl+Z as end-of-file is a CP/M-ism that DOS inherited, and Windows inherited it from DOS. It isn’t part of C++, it’s part of your operating system or your OS’s shell. It may interact (poorly) with your shell’s line input (which may be what you are running into). You’d be better off making small input files, and a.exe < input.txt rather than trying to mimic end-of-file with Ctrl+Z on the command line.


  • while (cin >> numAccout >> name >> balance) means process as long you are able to read all three data item: numAccout, name, balance. Location of end of file shouldn’t have impact on it. It should be always all or nothing.


  • 1

    Your title says “end-of-line”, your question says “end-of-file”, what are you actually asking about here? Please include a minimal reproducible example that actually compiles, and doesn’t contain unused variables.


  • @MarekR its not actually “all or nothing”. For example, extraction of numAccount might be fine, then extraction of name might fail, and because of that extraction of balance wouldnt happen.


Leave a Comment