0

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);
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by sid78669
0

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.

0

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!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.