So I'm trying to either
a) allow user to input a string until they type exit
or
b) redirect file from standard input(a.out < test.txt) until at end of file and then terminate
My attempt at the code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
string input = "";
while(true){
cout << "Enter string: ";
while(getline(cin, input)){
if(input == "exit" || cin.eof()) goto end;
cout << input << "\n";
cout << "Enter string: ";
}
}
end:
return 0;
}
This causes an issue with redirection, when I use command a.out < test.txt I get an infinite loop (where test.txt contains one line "hello")
User input seems to work fine
I'm using getline because in the actual program I need to read a file line by line and then manipulate the line before moving on to the next line of the file
EDIT: My question is, how do I terminate this loop accounting for both user input and redirection?