C : if-else about variables

标签 c if-statement

我是编程新手,我是C语言的初学者,我想问一下我在make if else时遇到的问题:

每种水果的代码..

Apple 的 A-001,售价 3 美元

A-002 适合 4 美元的葡萄

B-001 鳄梨,售价 2 美元

B-002 橙色,售价 1 美元

那么输入将是 数量、水果代码

输出将是 总计(数量*价格)

但是当我这样做时:

If (fruitcode=='A-001')

它告诉我字符常量太长..我应该使用什么水果代码?它只是整数还是字符还是什么?

这是我的代码:

q 表示数量 h 价格 K 代表水果代码

int main(){ int q,h,k,t;

printf("============================================\n");
printf("=                 Program                  =\n");
printf("============================================\n");
printf("     Fruit Code     Fruit       Prices \n");
printf("      A-001         Apple      Rp. 13.000 \n");
printf("      A-002         Grape      Rp. 16.000 \n");
printf("      B-001         Avocado    Rp. 10.000 \n");
printf("      B-002         orange     Rp. 12.000 \n");
printf("============================================\n");
printf("\n");
printf("  \n");
printf("============================================\n");
printf("=                    Form                  =\n");
printf("============================================\n");
printf("Transaction Time:  ");
scanf("%s", &t);
printf("Fruit Code : ");
scanf("%s", &k);

if(k=='A-001')
    printf("You Choose Apple");
}
else if(k='A-002'){
    printf("You Choose Grape");
}
else if(k='B-001'){
    printf("You Choose Avocado");
}
else{
    printf("You Choose orange");
}


printf("Quantity: ");
scanf("%s", &q);

getch();
return 0;
}

最佳答案

您需要对字符串使用双引号,而不是单引号。单引号仅适用于单个字符。

您也不能像这样比较字符串。您需要使用诸如 strcmp() 之类的东西。

关于C : if-else about variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35746642/

相关文章:

python - 如何正确设置 python/c 库?

javascript - 奇怪的条件行为 Javascript

python if wait then 语句

c - 在 C 中使用管道在进程之间发送浮点值

c - 如何使用 while 和 putchar 输出 char 数组

c - 在C程序中使用Unix别名命令

sql-server - UNION 与 IF 语句

java - 在 if 语句中检查数组的两个返回值

c++ - 为什么我在 for 循环中收到 'Else without previous if' 错误?

c - "Indexed"C 中的数组函数参数