OK, so I am redirecting the input from a file. The input is read into a string. The problem is, when the input file ends, the program still reads empty lines and goes into an endless loop. I have added an if statement for it to detect 5 empty entries and exit the loop. Anyway to just make the program understand that the input, if from a file, is an EOF?

#include <string>
using namespace std;

int main()
	string command = "";
	int blanks = 0;

		cout << endl;
		cout << "> ";


		std::getline(cin, command, '\n');
                //some code
                if ( command.length() <= 0 ){
				if(blanks > 5){
					cout << "No command was entered for 5 times. " << endl;
					cout << "Quiting..." << endl;
              }while(condition == true);

Use your getline statement to drive a while loop (I'm assuming you don't want the blanks you were just finding a way around them)

  //Other statements here

When there's no more input the loop should exit. Make sure that in your file you don't have extra blank lines at the end.

What does the code look like now and what does your input file look like?

Its like what you said, but I tried using it in a do while loop :P. It works great!


This question has already been answered. Start a new discussion instead.