简体   繁体   English

错误:“ {”令牌|之前的预期'=',',',';','asm'或'__attribute__'

[英]error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token|

There was a problem with this code in this link - 此链接中的此代码存在问题-
Why does it say 'expected declaration specifiers before 'main'' 为什么说'main之前的预期声明说明符

But the answers there were not satisfying. 但是那里的答案并不令人满意。 And there are a lot of problems that were not addressed at all. 而且有很多问题根本没有解决。

Here is some of the compiler output 这是一些编译器输出

|20|error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token  
|14|error: old-style parameter declarations in prototyped function definition  
|113|error: expected identifier or '(' before '{' token   
|139|error: expected '(' before '{' token  
|169|error: expected ':' or '...' before ';' token   
|276|error: redefinition of 'getFinalHand'   
|14|note: previous definition of 'getFinalHand' was here|

I really don't know how to deal with these. 我真的不知道该如何处理。

#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE 1

void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank[], int cardSuit[]);
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int 
ranksinHand[], int suitsinHand[])
int analyzeHand(int ranksinHand[], int suitsinHand[]);


main()
{
int bet;
int bank = 100;
int i;
int cardRank [5];
int cardSuit [5];
int finalRank[5];
int finalSuit[5];
int ranksinhand[13];
int suitsinhand[4];
int winnings;
time_t t;
char suit, rank, stillPlay;


printGreeting();


do{
bet = getBet();
srand(time(&t));
getFirstHand(cardRank, cardSuit);
printf("Your five cards: \n\n");
for (i = 0; i < 5; i++)
{
    suit = getSuit(cardsSuit[i]);
    rank = getRank(cardRank[i]);
    printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}

for (i=0; i < 4; i++)
{
    suitsinHand[i] = 0;
}
for (i=0; i < 13; i++)
{
    ranksinHand[i] = 0;
}

getFinalHand(cardRank, cardSuit, finalRank, finalSuit, ranksinHand, 
suitsinHand);

printf("Your five final cards:\n\n");
for (i = 0; i < 5; i++)
{
    suit = getSuit(finalSuit[i]);
    rank = getRank(finalRank[i]);
    printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}

winnings = analyzeHand(ranksinHand, suitsinHand);
printf("You won %d!\n\n", bet*winnings);
bank = bank - bet + (bet*winnings)
printf("\n\nYour bank is now %d.\n\n", bank);
printf("Want to play again? ");
scanf(" %c", &stillPlay);
}while (toupper(stillPlay) == 'Y');



 return;
 }

 /*************************************************************************/


 void printGreeting();
 {

 printf("**********************************************************\n\n");
 printf("\n\n\tWelcome to the Absolute Beginner's Casino\n\n");
 printf("\tHome of the Video Draw Poker");
 printf("**********************************************************\n\n");

 printf("Here are the rules\n");
 printf("You start with 100 credits, and you make a bet from");
 printf("1 to 5 credits.\n");
 printf("You are dealt 5 cards, and then you choose which ");
 printf("cards to keep");
 printf("or discard\n");
 printf("You want to make the best possible hand.\n");
 printf("\nHere is the table for winnings (assuming a ");
 printf("bet of 1 credit):");
 printf("\nPair \t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight \t\t\t4 credits");  
printf("Flush\t\t\t\t5 credits");
printf("Full House\t\t\t8 credits");
printf("Four of a Kind\t\t\t10 credits");
printf("Straight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n"); 
}


 void getFirstHand(int cardRank[], int cardSuit[]);
 {
int i,j;
int carDup;

for(i=0; i < 5; i++)
{
carDup = 0;
do{
    cardRank[i] = (rand() % 13);
    cardSuit[i] = (rand() % 4);

    for (j=0; j < i; j++)
    {
        if ((cardRank[i] == cardRank[j] &&
             cardSuit[i] == cardSuit[j]))
        {
            carDup = 1;
        }
    }
 }while (carDup == 1;);
 }
 }

 char getSuit(int suit)
 {
 switch
 {
case 0:
    return('C');
case 1:
    return('D');
case 2:
    return('H');
case 3:
    return('S');
}
}

char getRank(int rank)
{
switch (rank)
{
 case 0:
    return('A');
case 1:
    return('2');
case 2:
    return('3');
case 3:
    return('4');
case 4:
    return('5');
case 5:
    return('6');
case 6:
    return('7');
case 7;
    return('8');
case 8:
    return('9');
case 9:
    return('T');
case 10:
    return('J');
case 11:
    return('Q');
case 12:
    return('K');
}
}
int getBet()
{
int bet;
do
 {
printf("How much do you want to bet?(Enter a number");
printf("from 1 to 5, or 0 to quit the game): ");
scanf(" %d", &bet);

if (bet >= 1 && bet <= 5)
{
    return(bet);
}
else if (bet == 0)
{
    exit(1);
}
else
{
    printf("\n\nPlease enter a bet from 1-5 or ");
    printf("0 to quit the game\n\n");
}
}while ((bet < 0) || (bet > 5));
}

int analyzeHand(int ranksinHand[], int suitsinHand[])
{
 int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;


for (suit = 0; suit < 4; suit++)
if (suitsinHand[suit] == 5)
    flush = TRUE;
rank = 0;
 while (ranksinHand[rank] == 0)
rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
num_consec++;
if(num_consec == 5) {
straight = TRUE;
}
for (rank = 0; rank < 13; rank++){
if (ranksinHand[rank] == 4)
    four == TRUE;
if (ranksinHand[rank] == 3)
    three == TRUE;
if (ranksinHand[rank] == 2)
    pairs++;
}
if (straight && flush){
printf("Straight Flush\n\n");
return(20);
}
 else if (four){
printf("Four of a kind\n\n");
return (10);
}
else if (three && pairs == 1){
printf("Full House\n\n");
return (8);
}
else if (flush){
 printf("Flush\n\n");
return (5);
}
else if (straight){
printf("Straight\n\n");
return (4);
}
else if (three){
printf("Three of a Kind\n\n");
return (3);
}
else if (pairs == 2){
printf("Two Pairs\n\n");
return (2);
}
else if (pairs == 1){
printf("Pair\n\n");
return (1);
}
else{
printf("High Card\n\n");
return (0);
}
 }

void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int 
ranksinHand[], int suitsinHand[])
{
int i, j, carDup;
char suit, rank, ans;

for (i=0; i < 5; i++)
{
suit = getSuit(cardSuit[i]);
rank = getRank(cardRank[i]);
printf("Do you want to keep card #%d: %c%c", i+1, rank, suit);
printf("\nPlease answer (Y/N):");
scanf(" %c", &ans);
if (toupper(ans) == 'Y')
{
    finalRank[i] = cardRank[i];
    finalSuit[i] = cardSuit[i];
    ranksinHand[finalRank[i]]++;
    suitsinHand[finalSuit[i]]++;
    continue;
}
else if (toupper(ans) == 'N')
{
    carDup = 0;
    do{
        carDup = 0;
        finalRank[i] = (rand() % 13);
        finalSuit[i] = (rand() % 4);

        for (j=0; j < 5; j++)
        {
            if((finalRank[i] == finalRank[j]) && (finalSuit[i] == 
  finalSuit[j]))
            {
                carDup = 1;
            }
        }

        for (j=0; j < i; j++)
        {
            if((finalRank[i] == finalRank[j]) && (finalSuit[i] == 
finalSuit[j]))
            {
                carDup = 1;
            }
        }
    }while (carDup == 1);
    ranksinHand[finalRank[i]]++;
    suitsinHand[finalSuit[i]]++;
}

}
}

Please compare your program to a HelloWorld or something from a tutorial. 请将您的程序与HelloWorld或教程中的内容进行比较。 The usual head of main() (in the simplest case) should be int main(void) . main()的通常头(在最简单的情况下)应该是int main(void)

As a side note: 附带说明:
You need to add return statements to anything not returning void, eg lines 153, 188, 213 and to take care with WHAT you return in line 81. 您需要将return语句添加到不返回void的任何内容上,例如153、188、213行,并注意第81行返回的内容。
You need to take care with case-sensitivity of identifiers, eg lines 29, 30. 您需要注意标识符的区分大小写,例如第29、30行。
You need to decide on what you want to switch in line 142. 您需要在第142行中决定要切换的内容。
You need to see the difference between == and = in lines 240,242. 您需要在第240,242行中查看===之间的差异。
You seem to be fighting a war against semicolons ; 您似乎正在与分号作斗争; in lines 14, 16, 73, 116, 138, 173, please make peace. 在第14、16、73、116、138、173行中,请保持和平。

I am not saying that the program will work as expected after fixing those things, I did not care trying to figure that out. 我并不是说该程序在解决了这些问题后会按预期运行,我不在乎试图弄清楚这一点。 But if you copied from somewhere, it probably will. 但是,如果您从某个地方复制,则可能会复制。 Copying code this way is however unlikely to teach you anything. 但是,以这种方式复制代码不可能教给您任何东西。

In short, you need to learn making small working programs and extending them slowly, in small, verified steps. 简而言之,您需要学习制作小型的工作程序,并以经过验证的小步骤逐步扩展它们。
I cannot imagine how you managed to write code like that, over 300 lines, without intermediate steps. 我无法想象您如何在没有中间步骤的情况下,成功地编写了超过300行的代码。 The only thing I can imagine is that you copied this code from somewhere, manually typing. 我能想象的唯一一件事是您从某个地方复制了此代码,然后手动键入。 That seems consistent with the kind of mistakes in there. 这似乎与那里的错误类型一致。
Learn what the compiler warnings mean (using at least gcc -Wall ), by slowly working your way upwards through some tutorials. 通过逐步学习一些教程,了解编译器警告的含义(至少使用gcc -Wall )。 When manually copying tutorial code, you might make a few mistakes of this sort and have an easier life fixing them, one after the other. 手动复制教程代码时,您可能会犯这样的一些错误,并且一劳永逸地进行修复。
I recommend to play with tutorial code (after getting it right, to do what it supposed to), by changing little things. 我建议通过更改一些小东西来使用教程代码(正确理解之后,执行应做的事情)。 Eg change the output from "Hello world." 例如,更改“ Hello world”的输出。 to "Hi Praveen.", change the number of loops in a for (...;...;...) and things like that. 更改为“ Hi Praveen。”,请更改for (...;...;...)等中的循环数。 Get some practice that way, learn to predict which edits to code cause which changes to behaviour. 以这种方式进行一些练习,学习预测要对代码进行的编辑会导致行为的哪些更改。

Concerning StackOverflow, you should study the concept of 关于StackOverflow,您应该研究
https://stackoverflow.com/help/mcve https://stackoverflow.com/help/mcve
It is extremely helpful for yourself, being a great debugging technique. 它是一种出色的调试技术,对您自己非常有帮助。
And it is also very helpful for asking questions on StackOverflow. 这对于在StackOverflow上提问也很有帮助。 The exmple in this question is very far away from being "Minimal" for reproducing the problem with the head of main() . 这个问题的示例与用main()的头部重现问题的“最小”相距很远。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在&#39;:&#39;标记前应有&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token 错误:在'{'令牌之前预期'=',',',';','asm'或'__attribute__' - error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token :1:错误:在&#39;{&#39;标记之前应为&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - :1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 错误:在 '.' 之前应有 '='、','、';'、'asm' 或 '__attribute__' 令牌 - error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token 错误:在“*”标记之前应有“=”、“、”、“;”、“asm”或“__attribute__” - error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token 仍然是“错误:在&#39;*&#39;标记之前出现预期的&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39;” - Still the “error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token” 在&#39;&#39;之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39;。 代币 - expected '=', ',', ';', 'asm' or '__attribute__' before '.' token “在&#39;-&gt;&#39;标记之前的预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - "Expected '=', ',', ';', 'asm' or '__attribute__' before '->' token 在&#39;{&#39;令牌之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 错误:预期在“ void”之前出现“ =”,“,”,“;”,“ asm”或“ __attribute__” - error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM