在用户输入正确的值字段之前,我如何不断地反复询问用户一个问题?

时间:2019-07-19 12:00:28

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

我在这里是菜鸟编码器,我似乎无法弄清楚在这里添加什么以使其正确。应该再次询问用户是否不回答“是否要进行其他计算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的字符答案时,它将进入程序不应该包含的部分。

*这是我的第一个问题,所以我希望我已经正确完成了

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

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

我们首先将您的表述转化为更具技术性的形式,直到在语法和语义上起作用为止。让我们首先将其转换为:

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;
}
相关问题