-2

I am learning c++ and I am writing a card dealer program. When I compile my code and I get these errors:

dealer3.cpp:12: error: expected initializer before ‘int’
dealer3.cpp:33: error: expected constructor, destructor, or type conversion before ‘=’ token
dealer3.cpp:34: error: expected constructor, destructor, or type conversion before ‘=’ token
dealer3.cpp:35: error: expected constructor, destructor, or type conversion before ‘=’ token
dealer3.cpp:36: error: expected constructor, destructor, or type conversion before ‘=’ token
dealer3.cpp:37: error: expected constructor, destructor, or type conversion before ‘<<’ token
dealer3.cpp:38: error: expected declaration before ‘}’ token

and here is my code

#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<cmath>

using namespace std;

int randn(int n);
void draw();
int uni(int n);
char *suits[4]={"Hearts","Diamonds","spades","clubs"};
char *ranks[13]={"ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};
int drawn[52];
int remaining=52;
int main() {
    int n;
    int i;
    srand(time(NULL));
    while(1) {
       cout<<"enter number of cards to draw"<<endl;
       cin>>n;
       if (n==0) break;

for (i=1; i<=n; i++)
    draw();
}

    return 0;
}
int r;
int s;
int n;
int card;
n=randn(remaining--);
card=uni(n);
    r=card%13;
    s=card/13;
    cout<<ranks[r]<<" of "<<suit[s]<<endl;
}
int uni(int n)
{
int i=0;
while (drawn[i])
    i++;
while (n-->0){
    i++;
while (drawn[i])
    i++;
}
card_drawn([i])=true;
return i;
}
int randn (int n){
return rand()%n;
}

Why is this?

Kev
  • 118,037
  • 53
  • 300
  • 385
Nick Krichevsky
  • 61
  • 1
  • 3
  • 7

2 Answers2

6

Actually, this is a nice case where indenting the code would solve your problem (or make the solution very obvious), as it would show up several errors in with your braces. You have several lines of code that are outside of any function that don't belong there.

Some formatting hints for you:

  • Indent each nested block of code by a fixed amount of spaces (usually 4).
  • Leave a blank line after every function.
  • When opening a new block for a function or a for-, while- or if-statement (the list goes on), take care that you place your opening brackets consistently (the same style all over your code).
  • make sure that a bracket that closes a block is at the same indentation level as the statement/bracket that opened it.

Note that most IDEs have some option to automatically fix formatting for you (especially the indentation).

Braiam
  • 1
  • 11
  • 47
  • 78
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 2
    And in one fell swoop he declares the goodness of one bracketing style and the not-goodness of another. Do you have any idea how long that argument has been going on and how completely unresolved it is and will forever remain? ;-) – Carey Gregory Aug 13 '11 at 22:15
  • carey he got here first plus I cant mark a comment as a correct answer – Nick Krichevsky Aug 13 '11 at 22:20
  • 1
    @Nick: I'm not disputing his answer; his answer is correct. I'm simply making fun of him declaring one style of indentation "good" and another style "not good." Arguments have been waged for decades over that question and I assure you there is no agreed upon answer. – Carey Gregory Aug 13 '11 at 22:32
  • @Carey: Agreed. I much prefer the curlies under the `if`, `while`, etc., and I have no clue how this relates to the error. To each his own, so there is no "good" or "not good". – Rudy Velthuis Aug 13 '11 at 23:16
  • 1
    Ok, I changed that part to a recommendation about consistency. – Björn Pollex Aug 14 '11 at 08:32
1

You have a missing brace. And the following statement(s) goes in global space -

n=randn(remaining--);
// ...
Mahesh
  • 34,573
  • 20
  • 89
  • 115