gpt4 book ai didi

C++ 循环 : how to get program to repeat "error" message more than once anytime user enters invalid input

转载 作者:行者123 更新时间:2023-11-28 04:39:38 25 4
gpt4 key购买 nike

我几乎完成了这项作业,但仍然无法让代码连续显示错误消息,直到输入正确的输入。

例如,如果用户输入“4”进行操作(应该在 1-3 之间),它会正确显示:“您的操作选择无效!请使用 1、2 或 3 再试一次。 “但是,如果用户为操作输入了另一个无效数字(例如 5),它不会重复错误消息,而是继续前进。

谁能帮我弄清楚如何让每条错误消息重复,直到为每个提示输入有效的数字或字符?

注意:我对编码非常陌生,并且仍在弄清楚 stackoverflow...我想我已经遵循了所有 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.
*/

最佳答案

P29:练习算术技巧(if/else,循环)

描述:

“编写一个程序让 child 练习算术技能。​​

程序应该首先询问需要什么样的练习:+、-、*,然后让用户根据需要重复练习多次,直到输入“Q”。

将从 (0 - 9) 生成两个随机数。

如果 child 答对了方程式,就会出现一条消息,然后他们可以转到下一个问题(生成两个不同的数字)。

如果 child 回答不正确,应该会出现一条消息,并且应该重复这个问题(使用相同的数字)。”

终于修复了!:

    #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;
}

关于C++ 循环 : how to get program to repeat "error" message more than once anytime user enters invalid input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com