#include <stdio.h>
int main()
{
int age;
char marietal_status[15], sex[15];
printf("Age, Marietal status, sex of the driver : ");
scanf("%i %s %s", &age, marietal_status, sex);
if((marietal_status == married) || (marietal_status == unmarried && age > 30 && sex == male)
|| (marietal_status == unmarried && age >25 && sex == female))
printf("insured");
else
printf("uninsured");
}
輸出:
gcc /tmp/NaKWq16d9Q.c -lm
/tmp/NaKWq16d9Q.c: In function 'main':
/tmp/NaKWq16d9Q.c:10:28: error: 'married' undeclared (first use in this function)
10 | if((marietal_status == married) || (marietal_status == unmarried && age > 30 && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
| ^~~~~~~
/tmp/NaKWq16d9Q.c:10:28: note: each undeclared identifier is reported only once for each function it appears in
/tmp/NaKWq16d9Q.c:10:60: error: 'unmarried' undeclared (first use in this function)
10 | if((marietal_status == married) || (marietal_status == unmarried && age > 30 && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
| ^~~~~~~~~
/tmp/NaKWq16d9Q.c:10:92: error: 'male' undeclared (first use in this function)
10 | ried) || (marietal_status == unmarried && age > 30 && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
| ^~~~
/tmp/NaKWq16d9Q.c:10:152: error: 'female' undeclared (first use in this function)
10 | && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
| ^~~~~~
uj5u.com熱心網友回復:
married, unmarried,male和female是未宣告的物體。
看來您的意思是字串文字"married","unmarried"和."male""female"
但是像這樣的比較
marietal_status == "married"
將始終評估為 false,因為比較了兩個指標。
相反,您需要使用strcmp在 header 中宣告的 C 字串函式<string.h>。
所以 if 陳述句看起來
#include <string.h>
//...
if( ( strcmp( marietal_status, "married" ) == 0) ||
( strcmp( marietal_status, "unmarried" ) == 0 &&
age > 30 && strcmp( sex, "male" ) == 0 )
|| ( strcmp( marietal_status, "unmarried" ) == 0 && age >25 && strcmp( sex, "female" ) == 0 ) )
uj5u.com熱心網友回復:
在C你不能比較兩個char arrayusing ==。您必須使用strcmp如下功能:
#include <stdio.h>
#include <string.h>
int main()
{
int age;
char marietal_status[15], sex[15];
printf("Age, Marietal status, sex of the driver : ");
scanf("%i %s %s", &age, marietal_status, sex);
if(strcmp(marietal_status, "married") == 0 || (strcmp(marietal_status, "unmarried") == 0 && age > 30 && strcmp(sex, "male"))
|| (strcmp(marietal_status, "unmarried") == 0 && age >25 && strcmp(sex, "female") == 0))
printf("insured");
else
printf("uninsured");
}
uj5u.com熱心網友回復:
你試圖比較一個變數,編譯器告訴你它是未宣告的(這意味著它不存在,你沒有像 int age 那樣設定它,例如它是一個宣告的變數)。
處理字串時使用雙引號,"
例如初始化字串時,char sex[15] = "female";
所以你的 if 條件應該是這樣的:
if((strcmp(marietal_status, "married") == 0) || (strcmp(marietal_status, "unmarried") == 0) && age > 30 && (strcmp(sex, "male") == 0) || (strcmp(marietal_status, "unmarried") == 0) && age >25 && (strcmp(sex, "female") == 0))
運行它會產生這個:
./a.out
Age, Marietal status, sex of the driver : 23 unmarried male
uninsured
請注意,這意味著您希望用戶準確輸入此內容,否則比較將無法匹配。我建議閱讀 C 教程,https://www.tutorialspoint.com/cprogramming/index.htm
編輯:正如其他人已經指出的那樣,C 中的字串比較是由 strcmp 函式完成的,不要忘記 #include <string.h>
marietal_status == "unmarried" 可以在 Python 或 JavaScript 等其他語言中使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427571.html
