【问题标题】:Why does my program only loop 2 times? (Beginner C++)为什么我的程序只循环 2 次? (初级 C++)
【发布时间】:2017-11-06 00:19:10
【问题描述】:

我的问题在于代码末尾的 if/else if 语句。

  • if 语句应该在输入 Y 时无限循环,尽管它只运行两次并且不会要求用户在第二次运行时再次运行它

  • 如果用户输入 N,else if 语句应该完全关闭程序

  • else 语句应不断提示用户输入字符,直到输入有效字符。

-

 #include <iostream>
 using namespace std;

bool getData(int & width, int & height);

bool isDataValid(int & width, int & height);

void printBox(int & width, int & height);

int main()

{
int width = 0;
int height = 0;
bool validData = false;

getData(width, height);
isDataValid(width, height);
printBox(width, height);

while (validData == false)
{
    validData = getData(width, height);
}
system("pause");
return 0;
}
bool getData(int & width, int & height)
{
bool validData = true;
cout << "This program will draw a rectangular box in stars." << endl << endl << "The size of the box will be determined by the width and height" << endl << "that you specify.  " << endl << endl << "Enter integer values, because the width represents the " << endl << "numbe of columns, and the hieght represents the number of rows." << endl << endl << "The width should not exceed 79, because 80 is the " << "maximum screen " << endl << "width. Both width and height must be " << "at least 1. " << endl << endl;
cout << "Please enter a width:  ";
cin >> width;
cout << "Please enter a height:  ";
cin >> height;
cout << endl << endl;
return validData = isDataValid(width, height);
}
bool isDataValid(int & width, int & height)
{
if (width > 0 && width < 80 && height > 0)
{
    return true;
}
else
{
    cout << "Incorrect entry.\n\n";
    return false;
}


}
void printBox(int & width, int & height)
{
const int ROWS = height;
const int COLS = width;


for (int i = 0; i < ROWS; i++)
{
    for (int j = 0; j < COLS; j++)
    {
        cout << '*';
    }
    cout << endl;
}
char choice;
cout << endl << endl;

cout << "Do it again? (Y/N)" << endl;
cin >> choice;
while (toupper(choice == 'Y'))
{
 // repeat the program
}
if (toupper(choice == 'N'))
{
    cout << "Goodbye.\n\n";
 // close program
}

}

【问题讨论】:

  • toupper(choice == 'Y') 您将toupper 应用于比较的布尔结果,而不是应用于choice 值的字符。这没什么意义。我认为是no-op,条件相当于if (choice == 'Y')
  • "if 语句应该无限循环" If 语句不循环。循环语句循环。 printBox 中没有任何依赖于用户输入的内容。
  • 所以会是一个while循环?
  • 应该是某种形式的循环。我们将其作为练习留给读者选择。
  • 我将if 语句更改为while 循环,将else if 语句更改为if 语句并删除了else 语句,因为它对于赋值和只是我试图挑战自己,但这被证明是一个足够的挑战。不确定要包含哪些代码来重复程序或关闭程序而不返回。

标签: c++


【解决方案1】:

这里,是基本问题。为了获得重复的受控输出,某些东西必须在 while 循环内(使用 while 循环,因为在这些情况下很容易)。

  1. while 循环必须有输入语句,即您必须能够在循环内输入一些数据(否则会出现无限循环)。

现在,为了让您的程序请求多个输入,请将 getData 放入 while 循环中。

  1. 它必须显示预期的结果

所以,在你的情况下,将 printData() 放在 getdata() 之后的 while 循环中(因为输出在输入之后)

  1. 最后,询问用户是否要继续,并确保给出布尔语句(例如 .bool repeat = true),如果用户不想继续,则将其设置为 false。

您的最终代码应如下所示

int main()

{
int width = 0;
int height = 0;
bool repeat = true;

char ch;

while(repeat==true)
{
getData(width, height);
printBox(width, height);
isDataValid(width, height);
cout<<"\n\ndo you wish to continue?(y/n)\n"<<endl;
cin>>ch;
if(ch=='Y'||ch=='y')
continue;
else
{
cout<<"Goodbye!"<<;
break;             //you can also do repeat = false
}
}
}

并将您的 printData 函数更新为此

void printBox(int & width, int & height)
{
const int ROWS = height;
const int COLS = width;

for (int i = 0; i < ROWS; i++)
{
    for (int j = 0; j < COLS; j++)
    {
        cout << '*';
    }
    cout << endl;
}

}

其余的都是不必要的,不需要

【讨论】:

  • 我认为复制其他人的答案并将其发布在同一个问题上并稍作改动是不礼貌的!
  • 我没有复制。我们的方法是一样的。他刚刚把它贴在我面前
【解决方案2】:

您的代码中有一些错误:

  1. 首先你用false初始化validData,然后在你的循环中检查它是否仍然是false,然后再做一次,但你的目标是在数据有效或用户输入Y,因此将您的初始化更改为true,并更改您的while循环条件以检查它是否为true
  2. 你一开始调用了getDataisDataValid函数,没有必要这样做,你应该循环调用它们,顺便说一下你在getData中调用isDataValid,那么为什么要调用再来一次?
  3. 您忘记在循环内调用printBox 函数。
  4. 在函数printBox 中,您从用户那里获得一次输入,如果是Y,您将开始一个不定式循环!这不应该是你的目标,你想每次都问用户对吗?

因此,如果您遇到错误,可以像这样更改main

int main()
{
    int width = 0;
    int height = 0;
    bool validData = true;
    char choice = 'Y';
    while (validData == true && (choice == 'Y' || choice == 'y'))
    {
        validData = getData(width, height);
        printBox(width, height);
        cout << endl << endl;

        cout << "Do it again? (Y/N)" << endl;
        cin >> choice;
        if (choice == 'N' || choice == 'n') {
            cout << "GoodBye" << endl;
            break;
        }
    }

    system("pause");
    return 0;
}

并像这样更改printBox

void printBox(int & width, int & height)
{
    const int ROWS = height;
    const int COLS = width;

    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            cout << '*';
        }
        cout << endl;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多