我无法找到的编译器错误

时间:2017-10-15 17:37:03

标签: 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;
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 个答案:

答案 0 :(得分:1)

在while循环终止之前,需要关闭switch语句的花括号。

printf("Invalid choice\n"); break; } }while(choice != 3); 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;
}