C 我找不到编译器错误
我遇到了一个无法解决的错误。我已经彻底检查了我的代码,但没有成功。我做错了什么?请参阅下面的代码 编译器错误:C 我找不到编译器错误,c,compiler-errors,switch-statement,do-while,C,Compiler Errors,Switch Statement,Do While,我遇到了一个无法解决的错误。我已经彻底检查了我的代码,但没有成功。我做错了什么?请参阅下面的代码 编译器错误: In function 'main': ou1.c:49:1: error: expected 'while' before 'printf' printf("End of program!\n"); ^ 我的代码: #include <stdio.h> int main(void){ int choice; float price, sum, SUMusd; f
In function 'main':
ou1.c:49:1: error: expected 'while' before 'printf'
printf("End of program!\n");
^
我的代码:
#include <stdio.h>
int main(void){
int choice;
float price, sum, SUMusd;
float rate =1;
printf("Your shopping assistant");
do{
printf("1. Set exchange rate in usd (currency rate:%f)\n", rate);
printf("2. Read prices in the foreign currency\n");
printf("3. End\n");
printf("\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Give exchange rate: \n");
scanf("%f", &rate);
break;
case 2:
do{
printf("Give price(finsh with < 0)\n");
scanf("%f", &price);
sum =+ price;
}while(price <= 0);
SUMusd = sum*rate;
printf("Sum in foreign currency: %f", sum);
printf("Sum in USD:%f", SUMusd);
break;
default:
printf("Invalid choice\n");
break;
}while(choice != 3);
}
printf("End of program!\n");
return 0;
}
#包括
内部主(空){
智力选择;
浮动价格,金额,美元;
浮动利率=1;
printf(“您的购物助理”);
做{
printf(“1.以美元为单位设置汇率(货币汇率:%f)”,汇率;
printf(“2.读取外币价格”);
printf(“3.End\n”);
printf(“\n”);
scanf(“%d”,选择(&C);
开关(选择){
案例1:
printf(“给出汇率:\n”);
scanf(“%f”、&rate);
打破
案例2:
做{
printf(“给出价格(芬什语<0)\n”);
scanf(“%f”和“价格”);
总和=+价格;
}while(priceswitch语句的花括号需要在while循环终止之前关闭
printf(“无效选择\n”);
打破
}
}while(选项!=3);
printf(“程序结束!\n”);
已更正的完整代码示例
#include <stdio.h>
int main(void){
int choice;
float price, sum, SUMusd;
float rate =1;
printf("Your shopping assistant");
do{
printf("1. Set exchange rate in usd (currency rate:%f)\n", rate);
printf("2. Read prices in the foreign currency\n");
printf("3. End\n");
printf("\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Give exchange rate: \n");
scanf("%f", &rate);
break;
case 2:
do{
printf("Give price(finsh with < 0)\n");
scanf("%f", &price);
sum =+ price;
}while(price <= 0);
SUMusd = sum*rate;
printf("Sum in foreign currency: %f", sum);
printf("Sum in USD:%f", SUMusd);
break;
default:
printf("Invalid choice\n");
break;
}
}while(choice != 3);
printf("End of program!\n");
return 0;
}
#包括
内部主(空){
智力选择;
浮动价格,金额,美元;
浮动利率=1;
printf(“您的购物助理”);
做{
printf(“1.以美元为单位设置汇率(货币汇率:%f)”,汇率;
printf(“2.读取外币价格”);
printf(“3.End\n”);
printf(“\n”);
scanf(“%d”,选择(&C);
开关(选择){
案例1:
printf(“给出汇率:\n”);
scanf(“%f”、&rate);
打破
案例2:
做{
printf(“给出价格(芬什语<0)\n”);
scanf(“%f”和“价格”);
总和=+价格;
}while(如果没有适当的缩进,您的代码几乎无法理解。请修复此问题。(这样做时,您甚至可能会发现错误)@oxodo,我们在这里的做法不是标记标题[SOLVED]
。相反,如果您收到的答案为您充分解决了问题,请单击旁边的复选标记符号接受。StackOverflow鼓励您回答自己的问题。您可以这样做(并删除已解决的标记)。也许sum=+price;
应该是sum+=price;
。尽管sum
从未初始化过