error: expected ',' or ';' before '{' token.

Hi there. I'm new to programming and I saw somebody working on a blackjack game and decided to try it myself. I'm not too good yet, and I was wondering if someone could point out what I'm doing wrong. It seems to me that everything should work but again, I'm kind of new at this.

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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <time.h>
#include <math.h>

using namespace std;

int a, s, c, dealer, n;

string suit[4] = {"Heart","Club","Spade","Diamond"};
string number[13] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
int card_drawn[52];

int cards_remaining = 52;

int next_card(cards_remaining)
{
    int d=0;
    while(next_card[d])
    {
        d++;
    };
    while(cards_remaining-- > 0)
    {
        while(next_card[d])
        {
            d++;
        };
    };
    card_drawn[d] = true;
};

void draw_card(int a, int &s, int &c)
{
    int card;
    n = rand() % cards_remaining--;
    card = next_card();
    c = card % 13;
    s = card % 4;
    cout<<number[c]<<"of"<<suit[s];
    if(c>9)
    {
        c=9;
    };
    a = a+c+1;
    cout<<"Total: "<<a;
};

void dealer_draw_card(int a, int &s, int &c)
{
    int n, card;
    n = rand() % cards_remaining--;
    card = next_card();
    c = card % 13;
    s = card % 4;
    cout<<number[c]<<"of"<<suit[s];
    if(c>9)
    {
        c=9;
    };
    dealer = dealer+c+1;
    cout<<"Dealer has: "<<a;
};

int game()
{
    int player_num, dealer_num;
    char hit,again;
    for(player_num=0;playernum<2;player_num++)
    {
        draw_a_card(a,s,c);
    };
    ask:
    cout<<"Hit? (Y/N)"<<endl;
    cin>>again;
    if ((hit=='y')||(hit=='Y'))
    {
        draw_card(a, s, c);
        goto ask;
    };
    if (a>21)
    {
        cout<<"Player busts, dealer wins!";
        play_again();
    };
    for(dealer_num=0;dealer_num<2;dealer_num++)
    {
        dealer_draw_card(a,s,c)
    };
    dealer_ask:
    if (dealer<17)
    {
        dealer_draw_card(dealer, s, c);
        goto dealer_ask;
    };
    if(dealer>21)
    {
        cout<<"Dealer busts, player wins!";
        play_again();
    };
    if(a>dealer)
    {
        cout<<"Player wins!";
        play_again();
    }
    else
    {
        cout<<"Dealer wins!";
        play_again();
    };
    return 0;
};

void play_again(char again)
{
    cout<<"Would you like to play again?";
    cin>>again;
    if (again=='y'||again=='Y')
    {
        game();
    }
    else
    {
        exit(0);
    };
};


Code::Blocks is putting up one error. It says at line 18: "error: expected ',' or ';' before '{' token." I thought that because it was a function, it didn't need a semi-colon before the open bracket. Could someone please help me understand this?

edit: If you've seen some of this code before, its probably because I didn't write all of this by myself. I tried to learn through the post I saw online and kind of copied some of it and worked my way through it. This isn't for any kind of class, I'm just learning so I figured that was okay.
Last edited on
You need to give a type to the argument cards_remaining in the function next_card().
There are several issues (including kooth's post about next_card()):

You've peppered your code with semicolons that are not required! You only need a semicolon (";") after a statement. You do not require them after:

- while loops
- function/method definitions
- if & else statements

So you can start by getting rid of all the offending semicolons.

The code here (on line 20 & 26): while(next_card[d]) does not make sense. You are trying to invoke a function as if it were an array. You should pass the parameters inside round brackets like this: next_card(d). Also the function prototype for next_card specifies the return type int but you do not return a value.

On line 38 you have: next_card();. When you defined the function next_card(int cards_remaining) on line 17, you specify the parameter int cards_remaining. You cannot call this function without a parameter of type int because of the way you have defined it. You could overload the function, but I don't think that is what you intended (I'll provide a link at the end).

On line 70, in the condition part of your for loop, you misspelt player_num as playernum. Similarly on line 72 you misspelt draw_card as draw_a_card.

The definition of play_again(char again) appears at line 115 but you invoke the function on line 100, 105 & 110. This is an error as functions that are used must be defined above where they are called. Also you call play_again without passing any parameters, after specifying that play_again will take a char as the parameter.

Similarly you've called the function game inside of the play_again function, so it won't compile when you place it above the function game.

You can remedy this by using function prototypes at the top of your code (I'll provide a link). A function prototype tells the compiler that at some point in your code you are going to define a function with the prototype specified. For example:

1
2
3
4
5
6
7
8
int function(int param1);    //function prototype

//your code

int function(int param1)    //function definition
{
    //function body
}


On line 89 you forgot to put a semicolon after dealer_draw_card.

Also some of your variables are used without initialisation.

Finally your application lacks a main() function. Every C++ program (I think there might be a few exceptions), needs a main function. As it is the first entry point for all C++ applications.

Here are the links as promised:

Overloading Functions: http://en.wikipedia.org/wiki/Function_overloading
Function Prototypes: http://en.wikipedia.org/wiki/Function_prototype

Whoo, I think I've written a small novel.
Last edited on
Looking at your code it feels like it's a "first draft". I suspect that you might have raced through the coding part without compiling it step by step. When developing an application, it's important to write a small block of independent code, and testing it!

All programmers, even seasoned pro's miss the odd symbol and misspell variables and keywords, but by compiling your code a bit at a time, you can quickly discover these errors, rather than plowing through the whole thing and be overwhelmed with errors at the end.

Also, it might be worth while to re-examine the problem and re-examine your solution. I myself have found that I will change the design of an application because I realize that the method in which I am working is not the best way. This is totally normal in the Software Development process.

Don't be discouraged! Good luck with your next attempt.

P.S: Commenting your code is also a good idea. With comments you make clear your intentions in a particular segment of code so it's easier for you to understand if you need to look at your own code at a later point, and it also makes it easier for us to help you.

Comments: http://en.wikipedia.org/wiki/Comment_%28computer_programming%29
Last edited on
Right on, lnk2019! Qweasd, you have lots of nice people here to help you, so keep learning, keep coding, and keep having fun!
Thanks a lot! I'll work on that stuff, thanks! :)
Topic archived. No new replies allowed.