我如何一遍又一遍地向用户提问,直到他们输入正确的值字段?

How do I keep asking the user a question over and over again until they enter the correct field of value?

本文关键字:他们 输入 字段 何一遍 用户 一遍      更新时间:2023-10-16

我是这里的新手程序员,我似乎不知道要在这里添加到我的代码中以使其正确。如果用户没有正确回答"你想进行另一个计算Y还是N?"这个问题,它应该再次询问用户。我希望它重复要求用户输入 y 或 n,如果他们输入其他内容。我觉得很明显我只是想念它。这是为了学校,要清楚。

我尝试嵌套一个 do while 循环和一个 if 语句,但只是为了得到运行时错误

#include <iostream>
using namespace std;
int main() {
int base, exponent;
long int result = 1;
char choice;
int i;
do
{
cout << "This program raises a number to a specific power." << endl;
cout << "nEnter a base integer greater than 1: ";
cin >> base;
cout << "nEnter  an exponent integer to raise that number to: ";
cin >> exponent;
for (i = 1; i <= exponent; i++)
{
result = result * base;
}
cout << "n" << base << " to the power of " << exponent << " = " << result << endl;
result = 1; 
// ***** HERE IS WHERE I NEED HELP, WHAT TO 
//       DO IF THEY DONT ENTER Y OR N.....
cout << "nWould you like to make another calculation? Y or N: ";
cin >> choice;
cout << endl;
}
while (choice == 'y' || choice == 'Y');
cout << "Good bye, then. Have a good day.n" << endl;

return 0;
}

当我尝试添加一个嵌套的 do while 循环,并输入 y 或 n 以外的字符答案时,它会转到它不应该有的程序的一部分。

*这是我的第一个问题,所以我希望我做对了

您可以使用另一个do-while循环来包装输入部分。

do
{
cout << "This program raises a number to a specific power." << endl;
cout << "nEnter a base integer greater than 1: ";
cin >> base;
cout << "nEnter  an exponent integer to raise that number to: ";
cin >> exponent;
for (i = 1; i <= exponent; i++)
{
result = result * base;
}
cout << "n" << base << " to the power of " << exponent << " = " << result << endl;
result = 1; 
do
{
cout << "nWould you like to make another calculation? Y or N: ";
cin >> choice;
cout << endl;
} while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N');
}
while (choice == 'y' || choice == 'Y');

在这里学会有机地思考。让我做一个程序性的方法。

我们首先将您的公式转换为更具技术性的形式,直到它在语法和语义上起作用。让我们先把它转换成这样:

void process_things()
{
...
while(still_require_answer)
{
ask_for_answer();
}
...
}

这与你口头表达的方式非常接近,是吗?现在,让我们充实一下。

string ask_for_answer(bool& still_require_answer);
void process_things()
{
...
string answer = "";
bool still_require_answer = true;
while(still_require_answer)
{
answer = ask_for_answer(still_require_answer);
}
...
}
// hope you understand the concept of a reference here,
// that is what the ampersand (&) does, if not, ask
string ask_for_answer(bool& still_require_answer)
{
string answer = ""; // always initialize
cout << "State answer: ";
cin >> answer;
cout << endl;
if(answer == "Y" or ...)
{
still_require_answer = false;
}
return answer;
}

希望这对你有帮助。从长远来看,您可能希望使用OOP并在此处使用类。这里的代码有点冗长,但井然有序。

请注意,我已将例程放在一个新函数process_things中。任何超过几行的东西,你都应该考虑创建一个函数(或类方法)。您的main应该很小。将东西削减成更小的单元有助于保持井然有序,并使每个单元的设计变得容易(分而治之),并允许您更快地定位问题,因为您可以单独测试每个功能(稍后,这会导致自动单元测试)。

人们也可以花点时间把它放到它自己的功能string ask_until_valid_answer();,如果我们这样做,解散ask_for_answer并把它的内容放在那里。我想关注的是有机地拥有它,即使用自我描述的名称在阅读程序时解释程序,并将程序切割成可理解的单元。这将是另一种布局:

string ask_until_valid_answer();
void process_things()
{
...
string answer = ask_until_valid_answer();
...
}

string ask_until_valid_answer()
{
string answer = "";
bool still_require_answer = true;
while(still_require_answer)
{
cout << "State answer: ";
cin >> answer;
cout << endl;
if(answer == "Y" or ...)
{
still_require_answer = false;
}
}
return answer;
}