2016-11-09 60 views
0
#include <stdio.h> 

int main(){ 

    int age; 
    char gender; 
    printf("Are you male or female (m/f)?"); 
    scanf("%c",&gender); 
    printf("Enter your age:"); 
    scanf("%d",&age); 
    printf("\n"); 

    if(gender=="m"){ 
     if(age>=0 && age<=55){ 
     printf("You are a man in your best years!\n"); 
     } 
     else if (age>=56 && age<=100){ 
      printf("You are a wise man!\n");} 
     else{ 
    printf("An error occurred in the program!");} 
    } 
    else if(gender=="f"){ 
    if(age>=0 && age<=55){ 
     printf("You are a beauty!\n"); 
     } 
     else if (age>=56 && age<=100){ 
      printf("You look young for your age!\n");} 
     else{ 
    printf("An error occurred in the program!");} 

    } 
    else{ 
    printf("An error occurred in the program!");} 
    return 0; 
} 

該程序無法正常工作。它只打印最後的else語句,即「程序發生錯誤!」。我也試圖把所有條件相同的小括號僅打印「程序中發生錯誤!」

,如:

if(gender=="f"&& age>=0 && age<=55)) 

但仍沒有輸出預期的結果。

+3

'gender ==「m」' - >'gender =='m'' – BLUEPIXY

+1

通過類推,同樣適用於'else if(gender ==「f」)' –

+0

感謝它的工作。 – Nibesh

回答

3

您正在比較charstring文字(gender == "m")。雙引號是用於字符串的。在m (gender == 'm')和f (gender == 'f')附近使用單引號。

+0

感謝人,它工作得很好。欣賞它 – Nibesh

相關問題