C ++循环:如何让程序在用户输入无效输入时多次重复“错误”消息
Posted
技术标签:
【中文标题】C ++循环:如何让程序在用户输入无效输入时多次重复“错误”消息【英文标题】:C++ loops: how to get program to repeat "error" message more than once anytime user enters invalid input 【发布时间】:2018-05-24 23:55:44 【问题描述】:我几乎完成了这项任务,但仍然无法让代码连续显示错误消息,直到输入正确的输入。
例如,如果用户输入“4”进行操作(应该在 1-3 之间),它会正确显示:“您的操作选择无效!请重试,使用 1、2 或 3。 "但是,如果用户为该操作输入了另一个无效数字(例如 5),则不会重复错误消息,而是继续前进。
谁能帮我弄清楚如何让每条错误消息重复,直到为每个提示输入有效数字或字符?
注意:我对编码非常陌生,并且仍在弄清楚 ***...我想我已经遵循了所有 MCVE 建议/格式。谢谢你!!
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
temp = num1;
num1 = num2;
num2 = temp;
do
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " <<
endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
while (operation > 3 || operation < 1);
switch(operation)
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: "
<< endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
else if (guess == num3)
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "What is " << num1 << " - " << num2 << " ?: " <<
endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
cout << "That is incorrect. Please try again." <<
endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?:
" << endl;
cout << "" << endl;
cin >> guess;
else if (guess == num3)
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " <<
endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
cout << "That is incorrect. Please try again." <<
endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?:
" << endl;
cout << "" << endl;
cin >> guess;
else if (guess == num3)
cout << "That is correct!" << endl;
cout << "" << endl;
break;
do
cout << "Would you like to play again? Press Y for yes or Q for
quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
cout << "That is not a valid choice. Please choose Y for yes
or Q to quit. " << endl;
cout << "" << endl;
while(play !='Y' && play !='Q');
if (play == 'Y')
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
else
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
while(play=='Y');
return 0;
/*Sample Run:
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
3
You chose multiplication.
What is 4 * 1 ?:
4
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
1
You chose addition.
What is 6 + 1 ?:
7
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
2
You chose subtraction.
What is 5 - 0 ?:
5
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
1
You chose addition.
What is 7 + 1 ?:
9
That is incorrect. Please try again.
What is 7 + 1 ?:
10
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
2
You chose subtraction.
What is 7 - 3 ?:
5
That is incorrect. Please try again.
What is 7 - 3 ?:
6
Would you like to play again? Press Y for yes or Q for quit
Q
Thank you for playing! See you next time!
Process returned 0 (0x0) execution time : 43.057 s
Press any key to continue.
*/
【问题讨论】:
"。我想我已经遵循了所有 MCVE 建议/格式。" M 代表 M 初始,所以你的开关可以被移除,只是跳过你内心的do while
。
感谢您阅读help center(我想您看过?您已经知道minimal reproducible example),但您忘记关注this。如果您愿意,可以将其作为评论发布(尽管如果问题已经很好,理论上它不应该被否决)
【参考方案1】:
P29:练习算术技巧(if/else,循环)
说明:
“编写一个程序让孩子练习算术技能。
程序应该首先询问需要什么样的练习:+、-、*,并让用户根据需要重复练习多次,直到输入“Q”。
将从 (0 - 9) 生成两个随机数。
如果孩子正确回答了方程式,应该会出现一条消息,然后他们可以进入下一个问题(生成两个不同的数字)。
如果孩子回答错误,应该会出现一条消息,并且应该重复该问题(使用相同的数字)。”
终于修好了!:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
temp = num1;
num1 = num2;
num2 = temp;
do
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
if (operation > 3 || operation < 1)
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
while (operation > 3 || operation < 1);
switch(operation)
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
do
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
while (guess != num3);
if (guess == num3)
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "" << endl;
do
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
while (guess != num3);
if (guess == num3)
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "" << endl;
do
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
while (guess != num3);
if (guess == num3)
cout << "That is correct!" << endl;
cout << "" << endl;
break;
do
cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
cout << "" << endl;
while(play !='Y' && play !='Q');
if (play == 'Y')
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
else
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
while(play=='Y');
return 0;
【讨论】:
【参考方案2】:你的 do while 循环
do
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
while (operation > 3 || operation < 1);
应该是
do
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
while (operation > 3 || operation < 1);
【讨论】:
谢谢@Jarod42!这非常有帮助!【参考方案3】:如果您只想让用户重新尝试一次以选择正确的操作,请不要使用 do-while 循环。保持这种方式
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
或 如果您只想为第一次错误尝试和所有成功的错误尝试打印错误消息,您可以通过以下方式进行:
int flag=0;
do
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if ((operation > 3 || operation < 1)&&flag==0)
flag=1;
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
while(operation > 3 || operation < 1);
【讨论】:
以上是关于C ++循环:如何让程序在用户输入无效输入时多次重复“错误”消息的主要内容,如果未能解决你的问题,请参考以下文章
使用while循环获取Java factorial程序,以便在用户输入“y”并使factorial部分工作时继续重复