I found similar post from the forum, but I have another problem.
The code below I replaced ans=getch() with cin >> ans, but it fell into the next else(assuming user enter 'y') and printed. It was working fine and looped back to the start of the for loop if I keep ans=getch().
"Incorrect format
Would you like to try again? (Y/N)"
Shouldn't the program goes back to the start of the for loop instead of get into the else?
Code:
string fx;// store the user input of a term
char ans;// get user input for continue of exit
bool flagf=false;// keep track if polyf and polyg are zero
bool flagg=false;
bool has_entered;
for(;;)
{
system("CLS"); // Clear the console
cout << "\nAddition:\n\n" << endl;
cout << "Please enter each polynomial term in the format of: cx^k\n" << endl;
cout << "f(x)= ";
if (flagf)// Check if polyf is zero
{polyf.disp_poly();}// Display polyf if not zero
getline(cin, fx);// get user input of each term
if (check_string(fx)==true)// if user input is corrected
{
has_entered=addterm(fx,polyf);// check if the term exceeded MAX
if (has_entered)
{}
else// Exceeded MAX
{
cout<<"\n\nExceeded maximum degree of 19"<<endl<<endl;
system("PAUSE");
continue; // loop back to the beginging of for loop
}
flagf=true;
//Check if user want to add more terms
cout << "\n\n\n\n\n\n\n\n\nWould you like to add more term(s) to f(x)? (Y/N)";
cin >> ans;// wait for user input
if (ans=='N'||ans=='n')// if user entered 'N' or 'n' then break the for loop
{
break;
}
else if (ans=='Y'||ans=='y')// If user entered 'Y' or 'y' then do nothing
{;}
}
else// User input is not corrected
{ //check if user want to try again
cout << "\n\n\n\nIncorrect format" << endl;
cout << "\n\n\n\nWould you like to try again? (Y/N)";
ans=getch();// wait for user input
if (ans=='N'||ans=='n')
{
break; //If user entered 'N' or'n' the break the for loop
}
else if (ans=='Y'||ans=='y')
{}
}
}