I know how to create loop, but we have to create a loop and have the option of stopping at any time. I want my command to say "Press Y to leave command or else enter the 3 values":. its compiles, but when i press Y it starts to shows infinite answer.......... I want the command to close when i press Y....
This how it looks:
int main() {
int a, b, c;
int Y;
char close;
while ( close = Y ) {
cout << "Enter the 3 values representing the coefficients of a quadratic";
cout << " polynomial: " << endl;
cin >> a >> b >> c;
cout << " Press Y to leave command or else enter the 3 values: " << endl;
close = Y;
if (a==0) {
a = 1;
cout << endl << "First term cannot be zero; it has been set to 1" ;
cout << endl;
}
hasRealRoots( a, b, c);
func1 (a, b, c, hasRealRoots(a,b,c));
}
First off, what is int Y for? I think you wanted 'Y'. Second, you want close == ..., the comparison (for equality) operator, instead of close = ..., the assignment operator. Also cin >> a >> b >> c; whill gather three inputs in a row. If you want to give the user a change to quit by entering 'Y' you need to prompt them 3 separate times. And also, you can't gather the input 'Y' from cining an int value. You would need to collect the value as a string ad parse it. I would say look up atoi http://cplusplus.com/reference/clibrary/cstdlib/atoi but other people on here like using stringstreams http://cplusplus.com/reference/iostream/stringstream
About your problem: Remove the int Y. After the line where you ask for Y or 3 values (I yet don't get though what you mean by 3 values), add cin >> close; and make a conditional check using
1 2 3 4
if (close == 'Y' || close == 'y')
{
break;
}
This will end the loop. You should add this at the end of the loop.