2

I've never used EOF before and I'm wondering how I could create a code that continues running until I press Ctrl+D to activate EOF. This is the general idea I have:

int main(){
    int num;
    while (!EOF) { //while the EOF is not activate
        cin >> num; //use cin to get an int from the user
        //repeatedly give feedback depending on what int the user puts in
        //activate EOF and end the while loop when the user presses "Ctrl + D"
    }
}

So how would I set it up to end when the user presses Ctrl+D? Thanks!

Werner Henze
  • 15,279
  • 12
  • 41
  • 62
Zevvysan
  • 283
  • 1
  • 10

2 Answers2

3

Here is a working example using integers and the EOF check.

#include <iostream>

int main(int argc, char *argv[]) {

  int num;

  for (;;) {

    std::cin >> num;
    if (std::cin.eof()) break;
    std::cout << "Number is " << num << std::endl;

  }

  return 0;

}
Snohdo
  • 156
  • 5
2

Try

int main(){
    int num;
    while (cin >> num) { 
       // ...
    }
}
Jon Deaton
  • 2,626
  • 4
  • 21
  • 37