开发者

What are these errors? how can I fix them?

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: expec开发者_运维问答ted 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:

  • 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).


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

n=randn(remaining--);
// ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜