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?
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:
for-,while– orif-statement (the list goes on), take care that you place your opening brackets consistently (the same style all over your code).Note that most IDEs have some option to automatically fix formatting for you (especially the indentation).