【问题标题】:How do I keep asking the user a question over and over again until they enter the correct field of value?我如何不断地问用户一个问题,直到他们输入正确的值字段?
【发布时间】:2019-07-19 12:00:28
【问题描述】:

我是这里的新手编码员,我似乎无法弄清楚要在我的代码中添加什么以使其正确。如果用户没有回答“您要进行另一个计算 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
  • 不要把所有东西都塞进main,而是创建一个提示用户并返回结果的函数。

标签: c++ loops while-loop do-while


【解决方案1】:

您可以使用另一个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');

【讨论】:

  • 没错,这正是我需要的人。我非常感谢。
【解决方案2】:

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

我们首先将您的表述转化为更具技术性的形式,直到它在句法和语义上都有效。让我们先把它变成这样:

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

【讨论】:

  • 太棒了!我非常感谢这里的信息。我现在才开始讨论函数,所以我会在这里有一个很好的参考。非常感谢您帮助了一个苦苦挣扎的学生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
相关资源
最近更新 更多