一旦用户错了两次,就结束循环
Ending a loop once the user is wrong twice?
我正在做一个测试和测试用户。如果用户错了,他可以有第二次机会或跳过,如果他选择了第二次,但又错了,游戏就结束了。我该如何打破这个循环来结束游戏?我试着做了一个循环,
do { stuff} while (wrong<2) while counting ++wrong;
每次他都错了,但没有奏效。
我在下面用//
语句标记了++wrong
void player_try (string questions[][5], char answers[])
{
char user_guess;
int m = 0;
srand(time(NULL));
int x;
int choice;
int wrong =0;
for (m=0; m<7; m++)
{
do
{
x = (rand() % 7);
cout << user_name << ": Here is question number " << m+1 << endl;
cout << m+1 << ". " << questions[x][0]<< endl;
cout << "A. " << questions[x][1]<< endl;
cout << "B. " << questions[x][2]<< endl;
cout << "C. " << questions[x][3]<< endl;
cout << "D. " << questions[x][4]<< endl;
cin >> user_guess;
user_guess = toupper(user_guess);
while (!(user_guess >= 'A' && user_guess <= 'D'))
{
cout << "Please choose a valid answer.";
cin>> user_guess;
}
if (user_guess != answers[x])
{
cout <<"Wrong!" <<endl;
++wrong; // THIS IS WHERE I COUNT WRONG ONCE
cout << "Skip this question or take a chance at greatness?" << endl;
cout << "Press 1 to skip, press 2 to take a chance at greatness" << endl;
cin >> choice;
if (choice == '1')
{
cout << "we shall skip this question." << endl;
}
else
{
cout << "I applaud your bravery." << endl;
cout << user_name << ": Here is question number " << m+1 << endl;
cout << m+1 << ". " << questions[x][0]<< endl;
cout << "A. " << questions[x][1]<< endl;
cout << "B. " << questions[x][2]<< endl;
cout << "C. " << questions[x][3]<< endl;
cout << "D. " << questions[x][4]<< endl;
cin >> user_guess;
user_guess = toupper(user_guess);
while (!(user_guess >= 'A' && user_guess <= 'D'))
{
cout << "Please choose a valid answer.";
cin>> user_guess;
}
}
if (toupper(user_guess) != answers[x])
{
cout <<"Wrong!" <<endl;
++wrong;; // THIS IS WHERE I CANT WRONG TWICE
}
else
{
cout << "correct!" << endl;
}
}
else
{
cout << "correct!" << endl;
}
}
while(wrong < 2);
}
}
将函数返回类型更改为整数。这仅仅意味着将"void"改为"int"然后,在函数内部,将return 0;
放置在希望函数终止的点上。确保在用户也获胜的情况下包含另一个return 1;
。
main()函数就是这样工作的。考虑:
int main()
{
string tester = "some string";
if(tester == "some string")
return 1;
cout << "Hey!"
return 0;
}
在上述情况下,main()终止于"return 1;",因为if语句为TRUE。请注意,"嘿!"永远不会打印出来。它将以同样的方式为您的功能工作。
另外,您可以使用该返回值让OTHER函数(如main())知道函数终止是因为用户赢了(返回1)还是输了(返回0)。
是的,break语句也是终止循环的有效方法,但我认为这种方法是更安全、更干净的方法。通常,我们想知道函数或程序是否成功。
您可以使用break;如果该人两次答错了答案,则进行陈述。
根据意见。你可以放弃do-wile循环,而选择one-for-loop。如果错误的猜测是2
对于重构代码以消除重复工作,有几个很好的建议,但要使程序立即运行,必须突破围绕do { } while(wrong < 2)
循环的for
循环。
一个简单的方法是修改for循环来测试错误的变量。额外的好处是,如果我正确阅读了所有内容,您就不再需要do{ } while();
循环。
for (m=0; m<7 && wrong < 2; m++)
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 使用柯南打包时如何避免列出两次依赖?
- 为什么要执行两次位移((x >> 4)<< 4)?
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 对于语句结束两次
- 一旦用户错了两次,就结束循环