I’m trying to write a calculation program that, when it has run through once asks the user if they wish to make another calculation. This has the form of a separate function that is called at appropriate points in the main function:
char repeatcalc(char y){
cout << "Would you like to perform another calculation? [Y]es/[N]o" << endl;
cin >> y;
if(y == 'Y' || y == 'y'){
return y == 'y';
}
else if(y == 'N' || y == 'n'){
return y == 'n';
}
else{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
repeatcalc(y);
}
}
The idea being, the user hits a button and the function returns either a ‘y’, an ‘n’ or repeats itself. This then feed back into a while loop in the main function that repeats if a ‘y’ is returned and ends if an ‘n’ is returned.
The repeat section in the function above works in the main code, as does the ‘y’ return, but when ‘n’ is selected, it seems to return a ‘y’ anyway.
I’m missing something obvious, but I can’t figure out quite what! Any suggestions?
Thanks.
Convert your function to return a bool. For example:
…
being
ya local char variable. This way the function dont need an argument. And you can ask simpleif (repeatcalc() ) reapeat;Also, pay attention:
==is for comparing and return abool, while you are trying to make an assingment:return y='y';with in this case is justreturn 'y';