c++ - How to execute last line using getline -
i trying utilize getline read in each line separately file, lastly line in file not followed end of line deliminator getline not know has reached end of line.
i'm using terminal execute programme using ./evaluation < input0 input0 input file , lastly line in input0 read in isn't executed code. below trying do:
int main( int argc, char** argv ) { //reading in value user while( 1 ) { //starting output cout << "expression: "; string expressioninputs; string postfix; while( getline( cin, expressioninputs ) ) { break; } cout << expressioninputs << endl; //checks see if ctrl+d has been used input if( cin.eof() ) { cout << endl; break; } postfix = infixtopostfix( expressioninputs ); cout << "answer: " << postfixtoanswer( postfix ) << endl; } homecoming 0; } the programme suppose read in values such 1+2 , convert postfix ( 1 2 +) , evaluate answer.
you're interpreting outcome of std::basic_ios::operator bool() correctly, wonder if lastly line in file empty. in program, when std::getline() returns false, means wasn't able read characters before encountering either newline or eof.
note getline() returning cin here, , while statement evaluating operator bool() on it. operator bool() homecoming false if preceding operation failed, encountering eof not sufficient count failure—again, in case, unless there no characters available after preceding line.
do have sample input file can share here?
c++ getline
No comments:
Post a Comment