Search code examples
c++syntaxlabelgoto

error C2059: syntax error: '}' C++


void walka(Postac p, Przeciwnik e, int walkaa)
{
    if (p.szybkosc < 0)
    {
        p.szybkosc = 0;
    }
walka:
    walkaa = p.szybkosc - e.szybkosc;
    if (walkaa > 0)
    {
        do
        {
            cout << "Zadajesz " << p.sila << " obrażeń." << endl << endl;
            e.zycie -= p.sila;
            cout << "Życie: " << p.zycie << " Życie przeciwnika: " << e.zycie << endl << endl;
            if (e.zycie <= 0)
            {
                cout << "Wygrałeś!" << endl;
                goto koniecwalki;
            }
            walkaa -= e.szybkosc;
        } while (walkaa > 0);
        goto walka;
    }
    else
    {
        do
        {
            cout << "Otrzymujesz " << e.sila << " obrażeń." << endl << endl;
            p.zycie -= e.sila;
            cout << "Życie: " << p.zycie << " Życie przeciwnika: " << e.zycie << endl << endl;
            if (p.zycie <= 0)
            {
                cout << "Zostałeś pokonany." << endl;
                goto koniecwalki2;
            }
            walkaa += p.szybkosc;
        } while (walkaa < 0);
    }
    goto walka;
koniecwalki:
    cout << "Przegrana" << endl;
koniecwalki2:

} 

1>Others.cpp(202): error C2059: syntax error: '}'

202nd line is the last curly bracket at the end of this function. I don't know why I'm getting this error now. I had it earlier a few times but every time it was just random additional bracket which I had to remove.


Solution

  • The syntax error means that you have to use a null statement after the label

    koniecwalki2: ;
                 ^^^
    }
    

    That is it is a statement that can be labeled (in C++ declarations are also statements while in C declarations are not statements).

    Take into account that it is a bad idea to use goto statements. That makes the code difficult to read and modify.