2015-02-09 28 views
1
int menu() { 
    char choice [5]; 
    int i; 
    int c; 

    printf("Welcome to your own personal tamaguchi!"); 

    printf("\n 1.Name your tamaguchi.\n"); 
    printf("\n 2.Check health and age.\n"); 
    printf("\n 3.Feed tamaguchi.\n"); 
    printf("\n 4. Exercise with tamaguchi.\n"); 
    printf("Let tamaguchi sleep.n"); 
    printf("\n 5. Close program.\n"); 

    printf("Choose action: "); 
    scanf("%s", choice); 
    printf("\n"); 

    for (i=0; choice[i]! = '\0'; i++){ 
     if(!isdigit(choice[i])) 
      return -1; 
    } 

    c = atoi(choice); 
    return c; 
} 

他們說問題所在!其中choice[i]!='\0'我得到錯誤「預期」;''之前'!'令牌'

我已經包含了stdio,string,time和stdlib。 我不知道我做錯了什麼,如果你能看到錯誤,請告訴我?

謝謝。

+1

投票結束爲拼寫錯誤。 – dasblinkenlight 2015-02-09 14:45:03

+0

沒有長度檢查輸入.... – LPs 2015-02-09 14:45:59

+0

@dasblinkenlight它可能是一個錯字,我也投票關閉,但它也可能是OP認爲'!'和'='是獨立的操作符時使用'!= '。 – 2015-02-09 14:46:15

回答

3

您需要更改

for (i=0; choice[i]! = '\0'; i++){ 

for (i=0; choice[i] != '\0'; i++){ 
        ^
        | //notice here 

這裏的經營者是不等於!=。這是一個單一的操作員。

如果你的寫法類似於! = [它們之間有一個空格],那麼它就成爲兩個單獨的運算符,邏輯非和賦值,從而產生錯誤。

相關問題