Input Stream/ Loop Problem
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()
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
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..
vanalex
Junior Poster in Training
74 posts since Jan 2008
Reputation Points: 10
Solved Threads: 10
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?
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
For example, if the user entered "banana" instead of a valid expression, how could I get the program to restart itself?
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
SgtMe
Nearly a Posting Virtuoso
1,205 posts since Oct 2009
Reputation Points: 68
Solved Threads: 85
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.
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0