当我尝试使用递归时,我的C++程序不起作用了什么?

use*_*258 -2 c++ recursion continue


我需要以下c ++代码的帮助,尝试continue在程序的末尾添加一个,以便它为矩形创建用户指定的维度,并要求用户重新重做该程序.

在程序的最后部分编译并运行它没有愚蠢的if和else语句,并且它可以工作.但随着continue/递归,它失败了.LOLZ.我= noob.


int main()
{


    int height, width, tmp, tmp2;

    char continue;

    cout << "Please Enter The Height Of A Rectangle (whole numbers only): ";
height:
    cin >> height;
    if(height<1)
    {
        cout << "   Please Enter A Height Of Between 1 And 20: ";
        goto height;
    }
    cout << "Please Enter The Width Of A Rectangle  (whole numbers only): ";
width:
    cin >> width;
    if(width<1)
    {
        cout << "   Please Enter A Width Of Between 1 And 38: ";
        goto width;
    }

    cout << ' ';                                         // Add a space at the start (to neaten top)
    for(tmp=0; tmp!=width; tmp++) cout << "__";          // Top Of Rectangle
    for(tmp=0; tmp!=(height-1); tmp++)
    {
        cout << "\n|";   // Left Side Of Rectangle
        for(tmp2=0; tmp2!=width; tmp2++) cout << "  ";    // Create A Gap Between Sides
        cout << "|";
    }                                  // Right Side Of Rectangle
    cout << "\n|";                                       // Left Side Of Bottom Of Rectangle  to neaten bottom)
    for(tmp=0; tmp!=width; tmp++) cout << "__";          // Bottom Of Rectangle
    cout << '|';                                         // Right Side Of Bottom Of Rectangle (to neaten bottom)

    cout << "Type 'y' if you would like to continue and any other combination to quit.";
continue:
    cin >> continue;
    if(continue == 'y')
    {
        main();
        cout << "\n\n";
        system("PAUSE");
        return 0;
    }
    else
        cout << "\n\n";
    system("PAUSE");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Oli*_*rth 5

continue 是C++中的关键字,因此您不能拥有具有该名称的变量.


小智 5

你应该把你的代码放在一个while循环中.

int main()
{
    //  declaration of variables here

    do
    {
        // code here

        cout << "Type 'y' if you would like to continue and any other combination to quit.";
        cin >> doYouWantToContinue; // change the keyword!
    }
    while (doYouWantToContinue == 'y');
}
Run Code Online (Sandbox Code Playgroud)