Compiler error which I am unable to locate

筅森魡賤 提交于 2019-12-13 22:16:04

问题


I'm getting an error which I am not able to resolve. I've gone through my code thoroughly with no success. What am I doing wrong? See code below.

Compiler error:

In function 'main':
ou1.c:49:1: error: expected 'while' before 'printf'
 printf("End of program!\n");
 ^

My code:

#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:


The curly braces of the switch statement need to be closed before the while loop termination.

printf("Invalid choice\n"); break; } }while(choice != 3); printf("End of program!\n");

Corrected full code sample

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


来源:https://stackoverflow.com/questions/46758008/compiler-error-which-i-am-unable-to-locate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!