ComplexJS
ComplexJS

Reputation: 11

C++ syntax error. Missing a semi colon before a curly bracket on line 37

I was creating a simple C++ console game where you can input a bunch of commands, make money and buy things. As I was coding in the first few commands, I'm getting an error saying that I'm missing a ";" before "}" on line 37. I've ended every line with a semi colon, I don't seem to get the problem. I've put asterisks on line 37. Here's my code:

using namespace std;

void starting() {
cout << "***********DANK MEMER***********" << endl;
cout << "Enter Your Command: ";
}

int main()

{

starting();
int balance = 0;
bool hasShovel = false;
string command;

do {
    
    cin >> command;


    // /help
    if (command == "/help") {
        cout << "The list of commands are \n /buy shovel \n /buy lawnmower \n /sell 
shovel \n /sell lawnbower \n /luck \n \n /exit /rob \n /balance \n /invest \n /dig \n 
/mow" << endl;
    }

    // buy shovel
    else if (command == "/buy shovel") {

        // checking if they have enough balance
        if (balance > 100) {
            cout << "You don't have enough to buy a shovel. It costs 100 memers." << 
endl;
        }
*****************************************
        // giving them the shovel
        else {
            balance -= 100;
            hasShovel = true;
            cout << "You have a shovel now. Happy digging!" << endl;
        }
    }
    else {
        cout << "BRUH! Not a valid command";
    }
} while (command != "/exit");


}

Upvotes: 0

Views: 277

Answers (1)

Refugnic Eternium
Refugnic Eternium

Reputation: 4291

Running the code in https://www.onlinegdb.com/online_c++_compiler reveals the problem. There is a new line within your literal string in line 24. Newlines sometimes cause trouble, if they are unescaped (\).

Try it like this:

if (command == "/help") {
        cout << "The list of commands are \n /buy shovel \n /buy lawnmower\n"
        "/sell shovel\n"
        "/sell lawnbower\n"
        "/luck \n \n"
        "/exit /rob \n"
        "/balance \n"
        "/invest \n"
        "/dig \n" 
"/mow" << endl;

The preprocessor will concatenate the single string segments without any spaces, but that's what the \n are for.

This code compiles just fine for me:

#include <iostream>
#include <string>
using namespace std;

void starting() {
cout << "***********DANK MEMER***********" << endl;
cout << "Enter Your Command: ";
}

int main()

{

    starting();
    int balance = 0;
    bool hasShovel = false;
    string command;
    
    do {
        
        cin >> command;
    
    
        // /help
        if (command == "/help") {
                cout << "The list of commands are \n /buy shovel \n /buy lawnmower\n"
                "/sell shovel\n"
                "/sell lawnbower\n"
                "/luck \n \n"
                "/exit /rob \n"
                "/balance \n"
                "/invest \n"
                "/dig \n" 
            "/mow" << endl;
        }
    
        // buy shovel
        else if (command == "/buy shovel") {
    
            // checking if they have enough balance
            if (balance > 100) {
                cout << "You don't have enough to buy a shovel. It costs 100 memers." << endl;
            }
    
            // giving them the shovel
            else {
                balance -= 100;
                hasShovel = true;
                cout << "You have a shovel now. Happy digging!" << endl;
            }
        }
        else {
            cout << "BRUH! Not a valid command";
        }
    } while (command != "/exit");
}

Upvotes: 2

Related Questions