Expected ; before string constant and .

Firstly, a huge thank you to all forum members who have helped with my previous issues. Without their advice I would not be at around 2.5 k lines of code on my project. Unfortunately I have managed to anger Code::Blocks once again: this time while trying to shop. Before you see my function, I will summarize the error: the player can go to a village and buy/sell weapons/armor. When buying weapons, for example, all the village stock is displayed (contained in a vector), and the player chooses which one to buy. Sought item is displayed, along with the price, and the player is asked if he wants to proceed with the transaction. Here the error occurs: the compiler tells me, when I try to display the choice item, this: expected ; before string constant. Additionally, when working with the player's inventory (to sell), I have statements like player.inventory.begin(), for which a similar error occurs: expected ; before .
I have no idea what Code::Blocks wants, please help out. The shop() function, where the error occurs, is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
void Village::shop( Character player )
{
    int choice, price;
    char choice2;
    string item;

    system("cls");
    cout << "Welcome to the market!";

    do
    {

    cout << "\n1) Buy Weapons\n2) Buy Armor\n3) Sell Weapons\n4) Sell Armor\n5) Leave";
    cin >> choice;

    switch( choice )
    {
    case 1:
        system("cls");
        for(int i = 0; i <= Village::wealth; ++i)
        {
            Village::smithWeap.push_back( genWeapShop() );
            cout << i << ") " << Village::smithWeap[ i-1 ] << "\n";
        }
        cin >> choice;

        item = Village::smithWeap[choice - 1];

        price = priceGenerator( item );
        cout << "\n\nWould you like to buy " << item << " for " << price "? y/n\n\n";
        cin >> choice2;
        if( choice2 == 'Y' || choice2 == 'y' )
        {
            player.invWeapons.push_back( Village::smithWeap[choice - 1] );
            Village::smithWeap.erase( Village::smithWeap.begin() + (choice - 1) );
            player.gold -= price;
        }
        else
        {
            continue;
        }
        break;
    case 2:
        system("cls");
        for(int i = 0; i <= Village::wealth; ++i)
        {
            Village::smithArmo.push_back( genWeapShop() );
            cout << i << ") " << Village::smithArmo[ i-1 ] << "\n";
        }
        cin >> choice;

        price = priceGenerator( Village::smithArmo[choice - 1] );
        cout << "\n\nWould you like to buy " << Village::smithArmo[choice - 1] << " for " << price "? y/n\n\n";
        cin >> choice2;
        if( choice2 == 'Y' || choice2 == 'y' )
        {
            player.invArmor.push_back( Village::smithArmo[choice - 1] );
            Village::smithArmo.erase( Village::smithArmo.begin() + (choice - 1) );
            player.gold -= price;
        }
        else
        {
            continue;
        }
        break;
    case 3:
        system("cls");
        if ( player.invWeapons.empty() )
        {
            cout << "\n\nNothing to sell!\n\n";
            system("pause");
            continue;
        }
        else
        {
            for(int i = 0; i <= player.invWeapons.size(); ++i)
            {
                cout << i << ") " << player.invWeapons[ i-1 ] << "\n";
            }
            cin >> choice;

            price = priceGenerator( player.invWeapons[choice - 1] ) - (100 / player.agil);
            cout << "\n\nWould you like to sell " << player.invWeapons[choice - 1] << " for " << price "? y/n\n\n";
            cin >> choice2;
            if( choice2 == 'Y' || choice2 == 'y' )
            {
                Village::smithWeap.push_back( player.invWeapons[choice - 1] );
                player.invWeapons.erase([player.invWeapons.begin() + (choice - 1)]);
                player.gold += price;
            }
            else
            {
                continue;
            }
        }
        break;
    case 4:
        system("cls");
        if ( player.invWeapons.empty() )
        {
            cout << "\n\nNothing to sell!\n\n";
            system("pause");
            continue;
        }
        else
        {
            for(int i = 0; i <= player.invArmor.size(); ++i)
            {
                cout << i << ") " << player.invArmor[ i-1 ] << "\n";
            }
            cin >> choice;

            price = priceGenerator( player.invArmor[choice - 1] ) - (100 / player.agil);
            cout << "\n\nWould you like to sell " << player.invArmor[choice - 1] << " for " << price "? y/n\n\n";
            cin >> choice2;
            if( choice2 == 'Y' || choice2 == 'y' )
            {
                Village::smithArmo.push_back( player.invArmor[choice - 1] );
                player.invArmor.erase([player.invArmor.begin() + (choice - 1)]);
                player.gold += price;
            }
            else
            {
                continue;
            }
        }
        break;
    case 5:
        return;
    default:
        cout << "Error: no such option!\n\n";
        system( "pause" );
        continue;
    }
    }while ( choice != 5 );

    return;
}


Thank you in advance for all advice!
Last edited on
closed account (3hM2Nwbp)
player.invWeapons.erase([player.invWeapons.begin() + (choice - 1)]);

Removed invalid subscript operator. Can't compile this fragment to know more.

player.invWeapons.erase(player.invWeapons.begin() + (choice - 1));

A bit odd of a compiler output for this, you might want to move to clang. Here's what I get for a similar error:


???.cpp:30:14: error: expected variable name or 'this' in lambda capture list
    player.invWeapons.erase([player.invWeapons.begin() + (choice - 1)]);
                            ^
1 error generated. (note where the ^ is pointing!  clang usually points to the errors)
Last edited on
Could you please clarify? Where is this invalid subscript operator you speak of?
closed account (3hM2Nwbp)
Look at the first and second code blocks and compare them. (The operator itself is represented by square brackets '[ ]').
Last edited on
Ah, alright, thank you! What about the string display error? (Lines 30, 53, 83, 114 give me a "Expected ; before string constant" error)
You are missing some << operators after price on the first two, I'd guess you copy and pasted the error.
Ah, okay. I make a lot of stupid errors. Thank you!
Topic archived. No new replies allowed.