c++ - How to read multiple expressions from a file? -
i have following code
int main() { expression* expression; char paren, comma; string program; freopen("input", "r", stdin); while (true) { cout << "enter expression: "; cin >> paren; expression = subexpression::parse(); cin >> comma; parseassignments(); cout << "value = " << expression->evaluate() << endl; if(cin.eof()) break; } return 0; } void parseassignments() { char assignop, delimiter; string variable; int value; { variable = parsename(); cin >> ws >> assignop >> value >> delimiter; symboltable.insert(variable, value); } while (delimiter == ','); }
i want read multiple expressions file using cin only. no matter put cin.eof() in while loop, program reads input 1 more time , crashes. ahve tried cin.peek() still same output. suggest ways read eof.
as has been discussed countless times before, eof()
never right tool.
you want this:
expression * expression = nullptr; (char paren, comma; std::cin >> paren && (expression = subexpression::parse()) && std::cin >> comma && parseassignments(); ) { std::cout << "value: " << expression->evaluate() << "\n"; }
here assume both parse()
, parseassignments()
return that's convertible true
on success , false
on failure (like null pointer).
you might want insert checks delimiters expected, e.g. paren == '('
, comma == ','
.
Comments
Post a Comment