C++ 在不应该调用函数时调用函数
C++ Function being called when it isn't supposed to
我几乎完成了一个小的猜谜游戏,但我遇到了一个我不知道如何解决的问题。
问题出在check_guess函数上,该函数正在检查以确保输入的猜测是一个介于1和100之间的数字。
第一次运行程序时,一切正常。
https://i.stack.imgur.com/644yd.png(如果我的声誉不那么低,我会发布图片)
但每次选择"是"再次播放后,程序都会运行check_guess函数,并在不应该时显示"无效输入"
https://i.stack.imgur.com/E4rpb.png
我不知道这个程序为什么会这样。
整个程序的代码在这里:
#include <iostream>
#include <cstdlib> //for rand
#include <ctime> //for time
#include <string>
#include <sstream> //for conversions from string to int
using namespace std;
int check_guess(int tries) { //function for limiting the input of guess
string guess = "";
int result = 0;
do {
getline (cin, guess);
istringstream convert(guess);
if ( !(convert >> result) || (result < 1 || result > 100) ) {
result = 0;
cout << "Invalid Input.n" << endl;
cout << "You have " << tries << " tries: ";
}
} while (result == 0);
return result;
}
bool play_again() { //function for limiting the input of mode
bool quit;
string yn;
do {
cin >> yn;
if ( yn == "y" || yn == "yes" ) {
quit = false;
}
else if ( yn == "n" || yn == "no" ) {
quit = true;
}
else {
yn = "invalid";
cout << "Invalid input.nnEnter 'y' or 'n': ";
}
} while ( yn == "invalid" );
return quit;
}
int main()
{
srand(time(0)); //sets seed to be random
int mystery = 0; //defines mystery number
int guess = 0; //defines guess
int tries = 5; //defines trys
bool quit = false; //defines replay or quit
cout << "----------------------------------n";
do { //while mode is not set to quit, keep playing
tries = 5; //resets tries each new game
mystery = rand() % 100 + 1; //sets mystery number to be random
guess = 0;
cout << "Pick a number between 1 and 100.nnYou have 5 tries: ";
while (tries != 0) { //loops until you have no tries left
guess = check_guess(tries);
if (guess == mystery) { tries = 0; } //if you guess right it ends the loop
else { tries--; } //guessing wrong lowers tries by 1
if ( tries != 0 && guess > mystery) {
cout << guess << " is too high.n" << endl;
cout << "You have " << tries << " tries: ";
}
if ( tries != 0 && guess < mystery) {
cout << guess << " is too low.n" << endl;
cout << "You have " << tries << " tries: ";
}
}
if (guess == mystery) { //if guess == mystery by time loop ends you win
cout << "Got it! You Win!n" << endl;
}
else { //if not, you lose
cout << "You Lose! The number was: " << mystery << ".n" <<endl;
}
cout << "-------------------n";
cout << "Play Again?(y/n): "; //ask user to play again
quit = play_again();
cout << "-------------------n";
if (quit == false)
cout << endl;
} while (quit == false);
cout << "----------------------------------" << endl;
return 0;
}
我不知道该怎么解决。
此行:
cin >> yn;
只读取"y"而不读取行的末尾。因此,该指令的下一次执行
getline (cin, guess);
将猜测初始化为空字符串。
在第19行,导入不带引号的代码"cin.ignore();"。所以你的代码读起来是
`int check_guess(int trys){//用于限制猜测输入的函数string猜测=";int结果=0;
do {
getline (cin, guess);
istringstream convert(guess);
if ( !(convert >> result) || (result < 1 || result > 100) ) {
result = 0;
cin.ignore();
cout << "Invalid Input.n" << endl;
cout << "You have " << tries << " tries: ";
}
} while (result == 0);
return result;
}`
等等。这会短暂地停止对控制台的输入。您的代码正在读取"y"以在重新启动时再次尝试作为数字的输入。放入新行cin.ignore(),可以阻止它两次输入y。
将play_again()更改为:
bool play_again() { //function for limiting the input of mode
bool quit;
string yn;
do {
getline (cin, yn);
if ( yn == "y" || yn == "yes" ) {
quit = false;
}
else if ( yn == "n" || yn == "no" ) {
quit = true;
}
else {
yn = "invalid";
cout << "Invalid input.nnEnter 'y' or 'n': ";
}
} while ( yn == "invalid" );
return quit;
}
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?