【问题标题】:Compiler error which I am unable to locate我无法找到的编译器错误
【发布时间】:2017-10-15 17:37:03
【问题描述】:

我遇到了一个我无法解决的错误。我已经彻底检查了我的代码,但没有成功。我究竟做错了什么?请参阅下面的代码。

编译器错误:

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;
}

【问题讨论】:

  • 如果没有适当的缩进,您的代码几乎无法理解。请解决这个问题。 (这样做时,您甚至可能会发现错误)
  • @oxodo,我们这里不习惯标记标题[SOLVED]。相反,如果您收到的答案足以为您解决问题,请点击旁边的复选标记符号接受它。
  • StackOverflow 鼓励您回答自己的问题。您可以这样做(也可以删除 Solved 标签)。
  • 也许 sum =+ price; 应该是 sum += price;。虽然sum 从未初始化过

标签: c compiler-errors switch-statement do-while


【解决方案1】:

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

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多