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.
return y == 'n'
? – hmjd Feb 1 at 17:11else
block. – jrok Feb 1 at 17:12