简体   繁体   中英

C++ Compile Error

I can't figure out why I am getting these errors when I try to compile. I've never encountered the 'expected _ before _ token' error, but I believe they're common (if not feel free to enlighten me).

pe4.cpp: In function 'int main()' :
pe4.cpp:18: error: expected ')' before ';' token
pe4.cpp:18: error: expected ';' before ')' token
pe4.cpp:45: error: a function-definition is not allowed here before '{' token
pe4.cpp:51: error: a function-definition is not allowed here before '{' token
pe4.cpp:57: error: a function-definition is not allowed here before '{' token

#include <iostream>

using namespace std;

void printStar(int);
void printSpace(int);
void printNewLine();

int main()
{
    int side, i, j;

    if (i=0; i < 2; i++)
    {
        cout << "Enter side: " << endl;
        cin << side;

        if (side < 3 || side > 20)
        {
            cout << "Out of Bounds!!!"
            return 0;
        }

        printStar(side);
        printNewLine();

        {
            printStar(1);
            printSpace(side-2);
            printStar(1);
            printNewLine();
        }

        printStar(side);
        printNewLine();
    }

    void printStar(int a)
    {
        for (int j = 0; j < a; j++)
            cout << "*";
    }

    void printSpace(int a)
    {
        for (int j = 0; j < a; j++)
            cout << " ";
    }

    void printNewLine()
    {
        cout << endl;
    }
}

You have no ; at the end of the cout << "Out of Bounds!!!" line.

You have if (i=0; i < 2; i++) ; that should be for (i=0;... .

You have cin << side ; that should be cin >> side .

You have defined your function bodies inside main() ; they should live outside.

You are defining your functions printStar(), etc, inside your main() definition. Move those functions outside of main()'s closing bracket.

The closing } of the int main() method needs to go before void printStart(int a) .

Also, you need a ; at the end of cout << "Out of Bound!!!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM