2017-09-10 55 views
1

我搜索了論壇,我似乎無法找到適合我的具體問題的答案(我也嘗試過Google)。我似乎正確地比較了字符串(「是」,「是」,「否」,「否」)。我最初嘗試了一個if,但我認爲while循環更有效。有什麼建議麼?是的沒有C中的字符串

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

double far = 0; 
double cel = 0; 
double userValue = 0; 
double endResult = 0; 
int choice; 
char *decision = ""; 


int main() 
{ 
conversion(); 
return 0; 
} 

conversion() { 
    printf("Please enter a 1 for Celsius to Fahrenheit conversion OR\n a 2 
    for 
    Fahrenheit to Celsius conversion\n"); 

scanf("%d", &choice); 

    if(choice == 1) { 
    printf("Please enter a value for Celsius. Example 32 or 32.6\n"); 
    scanf("%lf", &userValue); 
    endResult = (userValue * (9.0/5.0) + 32); 
    printf("%lf\n", endResult); 
    yesOrNo(); 
} 

else 

printf("Please enter a value for Fahrenheit. Example 212 or 212.6\n"); 
scanf("%lf", &userValue); 
endResult = (userValue -32) * (5.0/9.0); 
printf("%lf\n", endResult); 
yesOrNo(); 

} 


yesOrNo() { 

printf("Do you want to continue? Enter Yes or No\n"); 
scanf(" %s", &decision); 

while(decision == "Yes" || decision == "yes") { 

    conversion(); 

} 

exit(0); 

} 
+0

不要使用==比較字符串使用'stricmp'而不是 – Abra001

+0

您需要使用'strcmp'來比較字符串的內容,否則只是比較它們是否在相同的地址。 –

+0

http://en.cppreference.com/w/c/string/byte/strcmp – Ghemon

回答

2

C沒有字符串。您必須使用函數strcmp()來比較字符串文字和/或以空字符結尾的字符數組。

decision == "Yes" 

應該

strcmp(decision,"Yes") == 0 
+0

我發佈後,我發現問題的時刻。事實上,我甚至無法在結案前提交我的回覆。 – user3062174

2

你不能比較使用==運營商需要使用strcasecmp()()或stricmp()功能,不區分大小寫字符串文字。
如果字符串相等strcasecmp()stricmp()返回0,如果第一個參數大於第二返回正數其他

+2

stricmp在這種情況下 – Abra001

+0

POSIX不區分大小寫的字符串比較函數是在''中聲明的'strcasecmp()',IIRC –

+0

@Jonathan Leffler,您剛剛檢查過,非常感謝! – coder

相關問題