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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -