错误:预期标识符或'('''''''令牌

时间:2014-06-22 00:02:51

标签: c compilation

我正在为一款名为MTG(Magic the Gathering)的游戏制作一个小型的生命和毒药计数器,我会解决一些不会让我编译的问题,任何人都可以提供帮助吗?对不起,如果他们很简单,我是C的新人......

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

int life = 20;
int poison = 0;
int amount;
char pn;
char lp;

int main(int argc, char* argv[]) {

while(1){
system("clear");
printf("Life:%d\nPoison Counters:%d\n\n\n", life, poison);
printf("\n\n<Life or Poison>(l/p) <positive or negative>(+/-) <amount>(#)\n\n\n>>>");
scanf("\n\n%c %c %d", &lp, &pn, &amount);
if(lp == 'l'){
    if(pn == '+'){
    life = life + amount;
    }
    else{
    life = life - amount;
    }
}else if(lp == 'p'){
    if(pn == '+'){
    poison = poison + amount;
    }
    else{
    poison = poison - amount;
    }
}else if(lp == 'q'){
break;
}
}
}
    }
return 0;
}

这就是终端告诉我的......

mtglife.c:36:2: error: expected identifier or ‘(’ before ‘}’ token
mtglife.c:37:1: error: expected identifier or ‘(’ before ‘return’
mtglife.c:38:1: error: expected identifier or ‘(’ before ‘}’ token

2 个答案:

答案 0 :(得分:0)

只需删除最后三行即可编译。但这并不意味着它必然是正确的。

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

int life = 20;
int poison = 0;
int amount;
char pn;
char lp;

int main(int argc, char* argv[]) {

    while(1){
        system("clear");
        printf("Life:%d\nPoison Counters:%d\n\n\n", life, poison);
        printf("\n\n<Life or Poison>(l/p) <positive or negative>(+/-) <amount>(#)\n\n\n>>>");
        scanf("\n\n%c %c %d", &lp, &pn, &amount);
        if(lp == 'l'){
            if(pn == '+'){
                life = life + amount;
            }
            else{
                life = life - amount;
            }
        }else if(lp == 'p'){
            if(pn == '+'){
                poison = poison + amount;
            }
            else{
                poison = poison - amount;
            }
        }else if(lp == 'q'){
            break;
        }
    }
    return 0;
}

答案 1 :(得分:0)

您尚未正确关闭主要功能。

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

int life = 20;
int poison = 0;
int amount;
char pn;
char lp;

int main(int argc, char* argv[]) {

    while(1){
        system("clear");
        printf("Life:%d\nPoison Counters:%d\n\n\n", life, poison);
        printf("\n\n<Life or Poison>(l/p) <positive or negative>(+/-) <amount>(#)\n\n\n>>>");
        scanf("\n\n%c %c %d", &lp, &pn, &amount);
        if(lp == 'l'){
            if(pn == '+'){
                life = life + amount;
            }
            else{
                life = life - amount;
            }
        }else if(lp == 'p'){
            if(pn == '+'){
                poison = poison + amount;
            }
            else{
                poison = poison - amount;
            }
        }else if(lp == 'q'){
            break;
        }
    }
    return 0;
}