0

我是 C 的新手,我制作这个游戏只是为了练习我已经知道的东西。我不太明白如何使代码从一个函数转到另一个函数(可能称为调用函数?)。我已经编译和测试了几次,但每次至少有一件事不起作用,我盲目地继续(可能不是一个好主意),现在我不能继续了。它还没有完成。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
    int response;
    int loyalty = 0;
    int level = 1;
    int howmany;
    int dothemnow;
    int squad1;
    int responsetwo;
    int pushup;
    char name [20];
int main(){
        pushup=1;
        dothemnow=0;
        howmany=0;
        printf ("Welcome to Game1\n");
        printf ("What's your name?\n");
        scanf ("%s",name);
        printf ("From now on you're Private %s\n",name);
        printf ("Loading...\n");
        sleep (2);
        printf("You are in a army excersize yard.\n");
        sleep (2);
        printf("Your sargeant approaches.\n");
        sleep (2);
        printf ("Sargeant Sam: Drop and give me five, Private %s\n",name);
        printf ("1)Yessir!\n2)Make me\n");
        scanf ("%d", &response);
            if(response==1){
                while(howmany<4 && pushup==1){
                    printf ("You do a pushup.\n");
                    howmany ++;
                    sleep(1);
                    printf ("You have done %d pushups\n",howmany);
                    printf ("Do another?\n1) yes\n2) no\n");
                    scanf ("%d",&pushup);
                }
            }

                if(howmany==5){
                    /*Make it so it goes to the war function*/
                    int war();
                }
                if(pushup==2 && dothemnow!=2){
                    dothemnow ++;
                    printf("Sargeant Sam: Do them NOW!!!\n");
                    sleep(2);
                    printf ("1)Why?\n2)Ok,ok\n");
                    scanf ("%d",&responsetwo);
                }
                    if(responsetwo==1){
                        while(howmany<4 && pushup==1){
                            printf ("You do a pushup.\n");
                            howmany ++;
                            sleep(1);
                            printf ("You have done %d pushups\n",howmany);
                            printf ("Do another?\n1) yes\n2) no\n");
                            scanf ("%d",&pushup);
                        }   
                    }       
                            if(howmany==5){
                                int war();
                            }
                            if(pushup==2){
                                dothemnow ++;
                            }
                    if(responsetwo==2){
                        while(howmany<4 && pushup==1){
                            printf ("You do a pushup.\n");
                            howmany ++;
                            sleep(1);
                            printf ("You have done %d pushups\n",howmany);
                            printf ("Do another?\n1) yes\n2) no\n");
                            scanf ("%d",&pushup);                   
                    }
                        }
                            if(howmany==5){
                            int war();
                            }
                            if(pushup==2){
                            dothemnow ++;
                            }
            if(response==2){
                dothemnow ++;
                printf("Sargeant Sam: Do them NOW!!!\n");
                sleep(2);
                printf ("1)Why?\n2)Ok,ok\n");
                scanf ("%d",&responsetwo);
            }   
                if(responsetwo==1){
                    dothemnow ++;
                }   
                if(responsetwo==2)
                    while(howmany<4 && pushup==1){
                        printf ("You do a pushup.\n");
                        howmany ++;
                        sleep(1);
                        printf ("You have done %d pushups\n",howmany);
                        printf ("Do another?\n1) yes\n2) no\n");
                        scanf ("%d",&pushup);
                        if(pushup==2){
                            dothemnow ++;
                        }
                        if(howmany==5){
                            int war();
                        }   
            }


        if(dothemnow==2){
            printf ("You are under military arrest!\n");
        }
return 0;
}
int war(){
    printf ("Loading...");
    sleep (2);
    printf ("Seargant Sam: Great job Private %s.\n",name);
    sleep (2);
    printf ("You are hereby promoted to Corporal");
    sleep (2);
    printf ("****LEVEL UP!****\n");
    sleep (1);
    level ++;
    printf ("You are now level %d!\n",level);
    sleep (1);
    printf ("Do you want to be friendly or strict to your squad?\n1) Friendly\n2) Strict\n");
    scan("%d", squad1);
    if(squad1==1){
        loyalty --;
    }
    if(squad1==2)
        loyalty ++;
    }
    printf ("Seargant Sam: You will be deployed to Afganistan in one week, Corporal %s.\n",name);
    sleep (1);
    printf ("In the mean time, get to know your sqaud.\n");
    sleep (1);
    printf ("There are ten about 20 to 25 year olds in front of you\n");
    sleep (1);
    printf ("1)Nod\n2)'Laugh' Well I guess I'm the boss of you now\n3)Salute\n4)Drop and give me twenty!\n");
    scanf ("%d", &squad2);
    switch(squad2){
        case 1:
            printf ("They nod back");
        case 2:
            loyalty - 2;
            printf ("They all smile and laugh a little");
            break;


        case 3:
            loyalty + 2;
            printf ("They all salute back");
            sleep(1)
            printf ("'As you were', you say");
            break;


        case 4:
            loyalty + 3;
            printf ("They do the pushups");
            break;
    }   
    printf ("Seargant Sam: Show them to their cabin, Corporal.");
    sleep (1);
    printf("You walk to the cabin.\n");
    sleep (1);
    if(loyalty>0){
        printf ("One of the soldiers in your squad, Private Smith, who looks wiry but strong, says 'Corporal, you seem like a cool guy, but how did you get promoted to corporal?'");
        sleep (1);
        printf ("Ha, I see how you could be confused");
        sleep (1);
        printf ("Well, I don't really know either, I just did five pushups, and wahla, Corporal.");
    }




}

所以,当我尝试编译这个时,我得到了错误:

game1.c:136: error: expected declaration specifiers or ‘...’ before string constant
game1.c:136: error: expected declaration specifiers or ‘...’ before ‘name’
game1.c:136: warning: data definition has no type or storage class
game1.c:136: error: conflicting types for ‘printf’
game1.c:136: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:137: error: expected declaration specifiers or ‘...’ before numeric constant
game1.c:137: warning: data definition has no type or storage class
game1.c:138: error: expected declaration specifiers or ‘...’ before string constant
game1.c:138: warning: data definition has no type or storage class
game1.c:138: error: conflicting types for ‘printf’
game1.c:138: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:139: error: expected declaration specifiers or ‘...’ before numeric constant
game1.c:139: warning: data definition has no type or storage class
game1.c:140: error: expected declaration specifiers or ‘...’ before string constant
game1.c:140: warning: data definition has no type or storage class
game1.c:140: error: conflicting types for ‘printf’
game1.c:140: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:141: error: expected declaration specifiers or ‘...’ before numeric constant
game1.c:141: warning: data definition has no type or storage class
game1.c:142: error: expected declaration specifiers or ‘...’ before string constant
game1.c:142: warning: data definition has no type or storage class
game1.c:142: error: conflicting types for ‘printf’
game1.c:142: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:143: error: expected declaration specifiers or ‘...’ before string constant
game1.c:143: error: expected declaration specifiers or ‘...’ before ‘&amp;’ token
game1.c:143: warning: data definition has no type or storage class
game1.c:143: error: conflicting types for ‘scanf’
game1.c:143: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:144: error: expected identifier or ‘(’ before ‘switch’
game1.c:166: error: expected declaration specifiers or ‘...’ before string constant
game1.c:166: warning: data definition has no type or storage class
game1.c:166: error: conflicting types for ‘printf’
game1.c:166: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:167: error: expected declaration specifiers or ‘...’ before numeric constant
game1.c:167: warning: data definition has no type or storage class
game1.c:168: error: expected declaration specifiers or ‘...’ before string constant
game1.c:168: warning: data definition has no type or storage class
game1.c:168: error: conflicting types for ‘printf’
game1.c:168: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
game1.c:169: error: expected declaration specifiers or ‘...’ before numeric constant
game1.c:169: warning: data definition has no type or storage class
game1.c:170: error: expected identifier or ‘(’ before ‘if’
game1.c:181: error: expected identifier or ‘(’ before ‘}’ token

请注意,这并不是说它在函数战争中。

4

1 回答 1

3

你的问题在这里:

 if(squad1==2)
        loyalty ++;
    }

你有一个右大括号,但没有一个左大括号。这个简单的语法错误导致了从这一点到文件末尾的一波编译错误。

请注意,第 136 行是第一个报告语法错误的点,是紧接在上述代码段之后的行。

于 2013-08-04T23:30:18.080 回答