我正在尝试编写一个计算程序,当它运行一次时,会询问用户是否希望进行另一次计算。这具有在主函数中的适当位置调用的单独函数的形式:
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);
}
}
这个想法是,用户点击一个按钮,函数返回一个“y”、一个“n”或重复自身。然后,这会反馈到 main 函数中的 while 循环,如果返回“y”则重复该循环,如果返回“n”则结束。
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.
我错过了一些明显的东西,但我无法弄清楚是什么!有什么建议么?
谢谢。