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;

	do{
		cout << endl;
		cout << "> ";

		command.clear();

		std::getline(cin, command, '\n');
                //some code
                if ( command.length() <= 0 ){
				blanks++;
				if(blanks > 5){
					cout << "No command was entered for 5 times. " << endl;
					cout << "Quiting..." << endl;
					break;
				}
              }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)

while(std::getline(cin,command,'\n'))
{
  //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.

Works like a charm :P

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

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!

Thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.