我如何一遍又一遍地向用户提问,直到他们输入正确的值字段?
How do I keep asking the user a question over and over again until they enter the correct field of value?
我是这里的新手程序员,我似乎不知道要在这里添加到我的代码中以使其正确。如果用户没有正确回答"你想进行另一个计算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;
}
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- C++:告诉用户输入 A、B 或 C,但如果他们输入不同的字符怎么办?
- 我如何一遍又一遍地向用户提问,直到他们输入正确的值字段?
- 提示再次运行程序:修复他们是否输入了无效语句
- 让用户提供输入,直到他们输入 X
- 如何提示用户输入数字,直到他们选择奇数?C++
- 提示用户输入正数,并继续提示用户,直到他们输入正数 c++
- 我想忽略用户的输入,如果他们越界
- 接受用户输入,无论他们在c++中输入大写还是小写
- 如果用户使用了我希望他们使用的值表之外的值,我如何提示他们重新输入?
- 用户可以输入许多数字,因为他们喜欢,将停止,如果他们将输入零,然后得到他们所输入的所有数字的总和