-3

I need help with the following c++ code, trying to add a continue at the end of the program so that it will make a user specified dimension for rectangle and ask the user to redo the program again.

Compile and ran it without the silly if and else statement at the final part of the program, and it works. But with the continue / recursion it failed miserably. lolz. me = 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;
}
razlebe
  • 7,134
  • 6
  • 42
  • 57
user352258
  • 45
  • 3
  • 10

6 Answers6

5

continue is a keyword in C++, so you cannot have a variable with that name.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
5

You should put your code in a a while loop.

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');
}
alizahid
  • 959
  • 6
  • 17
  • 1
    Your test fails to check whether the user entered something in the first place! If you just hit enter, `doYouWantToContinue` will be unchanged (and presumably uninitialized). – MSalters Jun 03 '11 at 10:39
4

In addition to continue being a reserved word, it is illegal to call main in C++. From the '03 standard, §3.6.1/3:

The function main shall not be used within a program. The linkage of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • +1 for mentioning both about `continue` being reserved and the illegality of calling `main`, AND for providing explanation. – razlebe Jun 03 '11 at 10:14
3

continue is used to short-circuit loops, e.g.:

for (i = 0; i < 10; ++i)
{
    if (f(i))
    {
        continue; // skip the rest of the loop
    }

    do_something_interesting_with(i);
}
Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45
2

continue is a c++ keyword, use a different name for it

instead of

char continue;

try

char cont;
Will
  • 871
  • 6
  • 16
0

My 2¢: throw away all this stuff, and rewrite it keeping in mind that

  1. gotos and labels are bad;
  2. they should be replaced with loops (probably do...while in your case);
  3. declaring variables/labels with reserved/already used names is bad (and in most cases it's illegal);
  4. recursing in main is bad (actually, it's illegal according to the standard);
  5. good indentation is not optional (even if the compiler won't mind).
  6. #includes are not optional.

And maybe move the user input/validation logic in a separate function to avoid code duplication.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299