我试图使用C创建一个简单的货币转换器,但由于某种原因,它一直给我错误"错误必须是可修改的左值"。 我检查了我的代码,但我没有看到任何问题。有什么帮助吗?
#include <stdio.h>
#include <stdlib.h>
void main(){
float jod_usd=1.41, usd_jod=0.71, jod_eur=1.26, eur_jod=0.8, currency;
char ic;
int f;
printf("Enter the number of your conversion option to continue...\n1- Convert from JOD\n2- Convert to JOD\n");
scanf("%d",&f);
printf("Enter the character of your second currency to continue...\nU - - USD\nE - - EUR\n");
scanf("%c",&ic);
printf("Enter your amount:\n");
scanf("%f",¤cy);
if(f==1&&(ic='U'||ic='u')){
printf("%.2f JOD - - %.2f USD\n",currency,(currency*jod_usd));
}
if(f==1&&(ic='E'||ic='e')){
printf("%.2f JOD - - %.2f EUR\n",currency,(currency*jod_eur));
}
if(f==0&&(ic='E'||ic='e')){
printf("%.2f EUR - - %.2f JOD\n",currency,(currency*eur_jod));
}
if(f==0&&(ic='U'||ic='u')){
printf("%.2f USD - - %.2f JOD\n",currency,(currency*usd_jod));
}
system("pause");
}
答案 0 :(得分:1)
TL; DR 使用=
代替==
的错误导致了这一点。
首先,您正在不必要地更改ic
的值。
其次,由于运营商的优先权,
(ic='U'||ic='u')
与
相同 ((ic='U'||ic) = 'u')
其中,(ic='U'||ic)
的结果不是左值。
答案 1 :(得分:0)
你确定它不仅仅是一个简单的错误吗?您通常不会分配条件。如果我解决了这个问题,那么你会得到一些程序流程:
Enter the number of your conversion option to continue...
1- Convert from JOD
2- Convert to JOD
1
Enter the character of your second currency to continue...
U - - USD
E - - EUR
Enter your amount:
代码
#include <stdio.h>
#include <stdlib.h>
void main(){
float jod_usd=1.41, usd_jod=0.71, jod_eur=1.26, eur_jod=0.8, currency;
char ic;
int f;
printf("Enter the number of your conversion option to continue...\n1- Convert from JOD\n2- Convert to JOD\n");
scanf("%d",&f);
printf("Enter the character of your second currency to continue...\nU - - USD\nE - - EUR\n");
scanf("%c",&ic);
printf("Enter your amount:\n");
scanf("%f",¤cy);
if(f==1 && (ic=='U' || ic=='u')){
printf("%.2f JOD - - %.2f USD\n",currency,(currency*jod_usd));
}
if(f==1&&(ic=='E'||ic=='e')){
printf("%.2f JOD - - %.2f EUR\n",currency,(currency*jod_eur));
}
if(f==0&&(ic=='E'||ic=='e')){
printf("%.2f EUR - - %.2f JOD\n",currency,(currency*eur_jod));
}
if(f==0&&(ic=='U'||ic=='u')){
printf("%.2f USD - - %.2f JOD\n",currency,(currency*usd_jod));
}
system("pause");
}