int main2()
{
 cin.get(); 
 cin.ignore();
 cin.clear();
 while(true)
 {
 long double x, y;
 char ch_op;
 Sleep(200);
 cout << "Enter an expression below: \nExamples:\n";
 cout << "x + y for Addition.\nx * y for Multiplication.\n" <<
 "x / y for Division\nx - y for Subtraction.\nx & x for Square Roots.\n" <<
 "x ^ y for Exponential Functions.\n\n";
 cin >> x >> ch_op >> y;
 switch(ch_op)
 {
  case '+':
       { add(x, y); main2(); break; }
  case '-':
       { subtract(x, y); main2(); break; }
  case '*':
       { multiply(x, y); main2(); break; }
  case '/':
       { divide(x, y); main2(); break; }
  case '&':
       {cout << sqrt(x) << "\n\n"; break; }
  }//switch
 }//while
  return 0;
}

int main(double z)
{
 long double x, y;
 char ch_op;
 cout << "Enter an expression below: \nExamples:\n";
 cout << "x + y for Addition.\nx * y for Multiplication.\n" <<
 "x / y for Division\nx - y for Subtraction.\nx & x for Square Roots.\n" <<
 "x ^ y for Exponential Functions.\n\n";
 cin >> x >> ch_op >> y;
 switch(ch_op)
 {
  case '+':
       { add(x, y); main2(); break; }
  case '-':
       { subtract(x, y); main2(); break; }
  case '*':
       { multiply(x, y); main2(); break; }
  case '/':
       { divide(x, y); main2(); break; }
  case '&':
       { cout << sqrt(x) << "\n\n"; main2(); break; }
  default:
          { cout << "Invalid Entry. Returning to Main Menu";
             Sleep(150); cout << "."; 
             Sleep(150); cout << " .";
             Sleep(150); cout << " ."; 
             main2(); 
             break; 
          }
  }//switch
  return 0;
}

In the above code, if you enter something Invalid at the very beginning, the program goes back to main2(), but keeps displaying the menu over and over and over. Is this a problem with clearing the input stream? Because I tried inserting a cin.ignore(255, '\n') and that didn't help, nor did cin.clear() or cin.get()

int main2()
{
 cin.get(); 
 cin.ignore();
 cin.clear();
 while(true)
 {
 long double x, y;
 char ch_op;
 Sleep(200);
 cout << "Enter an expression below: \nExamples:\n";
 cout << "x + y for Addition.\nx * y for Multiplication.\n" <<
 "x / y for Division\nx - y for Subtraction.\nx & x for Square Roots.\n" <<
 "x ^ y for Exponential Functions.\n\n";
 cin >> x >> ch_op >> y;
 switch(ch_op)
 {
  case '+':
       { add(x, y); main2(); break; }
  case '-':
       { subtract(x, y); main2(); break; }
  case '*':
       { multiply(x, y); main2(); break; }
  case '/':
       { divide(x, y); main2(); break; }
  case '&':
       {cout << sqrt(x) << "\n\n"; break; }
  }//switch
 }//while
  return 0;
}

int main(double z)
{
 long double x, y;
 char ch_op;
 cout << "Enter an expression below: \nExamples:\n";
 cout << "x + y for Addition.\nx * y for Multiplication.\n" <<
 "x / y for Division\nx - y for Subtraction.\nx & x for Square Roots.\n" <<
 "x ^ y for Exponential Functions.\n\n";
 cin >> x >> ch_op >> y;
 switch(ch_op)
 {
  case '+':
       { add(x, y); main2(); break; }
  case '-':
       { subtract(x, y); main2(); break; }
  case '*':
       { multiply(x, y); main2(); break; }
  case '/':
       { divide(x, y); main2(); break; }
  case '&':
       { cout << sqrt(x) << "\n\n"; main2(); break; }
  default:
          { cout << "Invalid Entry. Returning to Main Menu";
             Sleep(150); cout << "."; 
             Sleep(150); cout << " .";
             Sleep(150); cout << " ."; 
             main2(); 
             break; 
          }
  }//switch
  return 0;
}

In the above code, if you enter something Invalid at the very beginning, the program goes back to main2(), but keeps displaying the menu over and over and over. Is this a problem with clearing the input stream? Because I tried inserting a cin.ignore(255, '\n') and that didn't help, nor did cin.clear() or cin.get()

Hello Rickay, i think that your mistake is in loop:

while (true) {
   ....

}

This loop doesn't ever terminate..

I understand that, and I want it to be that way, so the user can continue to enter in expressions over and over as many times as they want. The problem is that the program doesn't wait for user input for the designation of x, ch_op, and y after each loop. That is what leads me to think I need to properly flush the input stream because there are characters leftover from the last cin >> if you gave an invalid expression. Any thoughts?

For example, if the user entered "banana" instead of a valid expression, how could I get the program to restart itself?

In general, I try to stay away from DOS commands... as they usually depend on the Operating system to work properly. And believe me, I checked out the sticky on flushing the input stream. None of the solutions it provided helped.

This article has been dead for over six months. Start a new discussion instead.