所以我想從命令行運行程序,格式爲: (./program -f)或(./program -c),具體取決於我是否想將一個數字從華氏到攝氏(-f)或攝氏到華氏(-c)。我遇到的問題是我收到錯誤/警告。我相信我的方法是正確的,但我仍然在發展我的技能。使用命令行查找方法
#include <stdio.h>
#include <string.h>
float c2f(float c);
float f2c(float f);
float c2f(float c)
{
return (9 * c/5 +32);
}
float f2c(float f)
{
return ((f - 32) * 5/9);
}
int main(int argc, char const *argv[])
{
char c[3];
char f[3];
strcpy(c, "-c");
strcpy(f, "-f");
char **p = &argv[1];
if(strcmp(p, c) == 0)
{
float returnc = c2f(atof(argv[2]));
printf("%f\n", returnc);
}
else if(strcmp(p, f) == 0)
{
float returnf = f2c(atof(argv[2]));
printf("%f\n", returnf);
}
else
printf("Wrong\n");
return 0;
}
這是我得到警告:
warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
char **p = &argv[1];
warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
if(strcmp(p, c) == 0)
note: expected ‘const char *’ but argument is of type ‘char **’
extern int strcmp (const char *__s1, const char *__s2)
warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
float returnc = c2f(atof(argv[2]));
warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
else if(strcmp(p, f) == 0)
note: expected ‘const char *’ but argument is of type ‘char **’
extern int strcmp (const char *__s1, const char *__s2)
我已經跑了我的代碼和它只是默認的「錯誤」,這意味着它不能識別-f/-c。
如果您收到錯誤/警告消息,包括問題!!! <是的,我在喊> – John3136
什麼是錯誤信息和警告。將它們編輯成你的問題。 – DeiDei
@ John3136謝謝你的建議。我已經添加了警告 –