Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在用户输入正确的值字段之前,我如何反复询问用户一个问题?_C++_Loops_While Loop_Do While - Fatal编程技术网

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

C++ 在用户输入正确的值字段之前,我如何反复询问用户一个问题?,c++,loops,while-loop,do-while,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

我是这里的新手,我似乎不知道要在这里添加什么才能使代码正确。如果用户没有正确回答“您想再次计算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;
}
#包括
使用名称空间std;
int main(){
int基,指数;
长整数结果=1;
字符选择;
int i;
做
{
库特指数;

对于(i=1;i您可以使用另一个
do while
循环来包装输入部分

do
{
库特指数;

对于(i=1;i在这里学习有机地思考。让我做一个程序性的方法

我们首先将您的公式转换为一种更技术化的形式,直到它在语法和语义上起作用。让我们首先将其转换为:

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

在另一个
do while
中包装提示,而不是在
main
中填充所有内容,创建一个函数来提示用户并返回结果。没错,这正是我需要的。我非常感谢。太棒了!我真的很感谢这里的信息。我刚刚开始使用函数,所以我会有一个很好的结果非常感谢你帮助了一个苦苦挣扎的学生。
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;
}