Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...