John
John

Reputation: 10029

Proper use of EOF (can it be used multiple times in a program?)

In my intro to programming course (c++), the instructor told us to:

"Write a program that inputs an unspecified number of integer ("int") values and then outputs the minimum value and the maximum value that were entered. At least one value will always be input. (read all input until EOF). For extra credit make your program also work when the user doesn't enter any ints at all (just the EOF.)"

I wanted to get fancy, so, in my solution, when just EOF is entered, the program responds with "Oops! You didn't enter anything. Please try again, this time entering at least one integer: " and prompts for input again.

My instructor is saying that this answer is wrong because

After the EOF, there should be no more input to a program (neither expected by the user nor the program) — using the EOF to switch from “one mode” of input to another mode of input isn’t supporting the standards.

  1. Every definition of EOF I've found on the internet doesn't seem to support my professor's definition. EOF, from what I can tell, is simply defined as the end of the current file. It seems perfectly valid to accept input from a user until EOF, do something with that input, and then ask for additional input until EOF again.
  2. Because this is an online course, I was able to review everything we learned relating to EOF and we were only told that EOF meant "End of File" and could be 'used to signal an end to user input' (important, because, even if my professor was wrong, one could argue that I should have adopted his standards if he had specifically told us to. But he didn't tell us to).

What is the proper way to use EOF with user input? Is my professor's statement that "After the EOF, there should be no more input to a program" the standard and expected way to use EOF? If a program accepts a variable amount of input, does something with it, and then accepts more variable input, is it not acceptable to use EOF with those inputs (aka don't use while(cin >> user_input) in that scenerio)? If so, is there a standard for what should be used to signal end of input in a scenario where you're accepting variable input multiple times?

My exact solution to the assignment is below. My solution to the main assignment "Write a program that inputs an unspecified number of integer ("int") values and then outputs the minimum value and the maximum value that were entered" was considered correct, by the second part of the assignment "make your program also work when the user doesn't enter any ints at all (just the EOF.)" was deemed incorrect ("make the program also work" is the only prompt we were given).

Thanks so much for any feedback!! Obviously, I'm skeptical of my professors feedback / decision, but, in general, I'm just trying to get a sense of C++ community standards.

#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>

using namespace std;

int main(){
  string user_input;

  int int_input, min_user_input, max_user_input;
  bool do_it = true;

  cout << "Hi John," << endl;
  cout << "Please enter a few integers (signal EOF when finished): ";

  while(do_it) {
    cin.clear();
    cin >> user_input;

    if (user_input.empty()) {
      cout << endl;
      cout << "Oops! You didn't enter anything. Please try again, this time entering at least one integer: ";
    }
    else {
      try {
        int_input = atoi( user_input.c_str() );

        min_user_input = int_input;
        max_user_input = int_input;

        while(cin >> int_input) {
          if (min_user_input > int_input) {
            min_user_input = int_input;
          }

          if (max_user_input < int_input) {
            max_user_input = int_input;
          }
        }

        cout << endl;
        cout << "The max user input was: " << max_user_input << endl;
        cout << "The min user input was: " << min_user_input << endl;
        do_it = false;
      }
      catch (std::invalid_argument) {
        cout << endl;
        cout << "Oops! You didn't enter an integer. Please try again, this time only entering integers: ";
        do_it = true;
      }
    }
  }

  return 0;
}

Note: additional feedback I got on this submission was: to not use c libraries (apparently stdlib.h is one) and that, on some computers (though, apparently, not mine), #include <stdexcept> will be needed to compile.

Answer

Short answer: my instructor is correct. When used with cin, no additional user input should follow an EOF signal. Apparently, in some cases the program won't let you enter more information, but, as @hvd points out

Although your system may let you continue reading from the same file in the specific case that it is coming from a TTY, due to EOF being faked there, you shouldn't generally rely on that.

Aka, because I'm using a terminal to enter user input, the program happens to work. In general, it won't work though.

As @RSahu answers, EOF just shouldn't be used to signal the end of variable length cin multiple times in a program. Importantly

There is no standard means, or commonly practiced coding standard, of indicating when user input has ended for the time being. You'll have to come up with your own mechanism. For example, if the user enters "end", you can use it to deduce that the user has ended input for the time being. However, you have to indicate to the user that that's what they need to enter. Of course, you have to write code to deal with such input.

Because this assignment required the use of EOF, what I was attempting to accomplish was, unintentionally, prohibited (aka receive input, check it, possibly receive more input).

Upvotes: 2

Views: 1644

Answers (2)

user743382
user743382

Reputation:

EOF, from what I can tell, is simply defined as the end of the current file.

It is. Note that in particular, what it doesn't mean is the automatic start of a new file.

Although your system may let you continue reading from the same file in the specific case that it is coming from a TTY, due to EOF being faked there, you shouldn't generally rely on that. Try program </dev/null and see happens when you try to automate your program.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206577

Proper use of EOF (can it be used multiple times in a program?)

There is no single EOF. There is EOF associated with every input stream.

If you are reading from a file, you can reset the state of the std::ifstream when it reaches EOF to allow you to read the contents of the file again.

However, if you are reading data from std::cin, once EOF is reached, you can't read from std::cin any more.

In the context of your program, your professor is right. They are most likely talking about reading from std::cin.

Upvotes: 7

Related Questions