我正在学习c ++而我正在写一个卡片经销商计划。当我编译我的代码时,我得到了这些错误:
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
这是我的代码
#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;
}
为什么会这样?
答案 0 :(得分:6)
实际上,这是一个很好的案例,其中缩进代码可以解决您的问题(或使解决方案非常明显),因为它会显示您的大括号中的几个错误。你有几行代码不在任何属于那里的函数之外。
为您提供一些格式提示:
for
- ,while
- 或if
语句打开新功能块时(列表会继续显示),请注意保持开括号的一致性(代码中的样式相同)。请注意,大多数IDE都有一些选项可以自动为您修复格式(尤其是缩进)。
答案 1 :(得分:1)
你有一个缺失的大括号。以下声明载于全球空间 -
n=randn(remaining--);
// ...