我搜索了論壇,我似乎無法找到適合我的具體問題的答案(我也嘗試過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);
}
不要使用==比較字符串使用'stricmp'而不是 – Abra001
您需要使用'strcmp'來比較字符串的內容,否則只是比較它們是否在相同的地址。 –
http://en.cppreference.com/w/c/string/byte/strcmp – Ghemon