Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

c - Compiler error which I am unable to locate

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!
");
 ^

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)
", rate);
printf("2. Read prices in the foreign currency
");
printf("3. End
");
printf("
");
scanf("%d", &choice);

switch(choice){
case 1:
printf("Give exchange rate: 
");
scanf("%f", &rate);
break;

case 2:

do{

printf("Give price(finsh with < 0)
");
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
");
break;
}while(choice != 3);
}
printf("End of program!
");



  return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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)
", rate);
printf("2. Read prices in the foreign currency
");
printf("3. End
");
printf("
");
scanf("%d", &choice);

switch(choice){
case 1:
printf("Give exchange rate: 
");
scanf("%f", &rate);
break;

case 2:

do{

printf("Give price(finsh with < 0)
");
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
");
break;
}
}while(choice != 3);

printf("End of program!
");



  return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...