# Thread: Problem on - Selection on which item to purchase

1. ## Problem on - Selection on which item to purchase

The question -

Write a program that gets user selection on which item to purchase. Display the proper menu for this. User will enter P to select Pie and C to select Cake.

If user selects Pie, get user selection for 3 choices.
1  Apple Pie (RM 2.50)
2 - Chicken Pie (RM 4.00)

If user selects Cake, get user selection for 4 choices.
1  Chocolate Cake (RM 3.50)
2  Honey Fruit Cake (RM 3.80)
3  Prune Cake (RM 3.20)

Once the selection has been made, get user input for quantity. Display the total amount that user have to pay and item that user has selected. See the sample program execution below to guide you in writing the program.

Sample 1:

Enter [P] to select Pie
Enter [C] To select Cake
P

Enter the number to make a selection
'1' - Apple Pie (RM2.50)
'2' - Chicken Pie(RM4.00)
2

You've selected Chicken Pie

The total price is RM 40.00

Sample 2:
Enter [P] to select Pie
Enter [C] To select Cake
C

Enter the number to make a selection
'1' - Chocolate Cake (RM 3.50)
'2' - Honey Fruit Cake (RM 3.80)
'3' - Prune Cake (RM 3.20)
1

You've selected Chocolate Cake

The total price is RM 17.50

The problem is i can't compile.
this is my code :

Code:
```#include<stdio.h>
#include<string.h>

int main()
{
int choice, quantity ;
float total_price, price;
char apple_pie, chicken_pie, beef_pie, chocholate_cake, honey_fruit_cake, prune_cake;

printf("\n Enter [P] to select Pie     :   ");
printf("\n Enter [C] to select Pie     :   ");
scanf("%d" ,&choice);

while (choice=='p')
{
printf("\n '1' - Apple_pie   (RM2.50) ");
printf("\n '2' - Chicken_pie (RM4.00) ");
printf("\n '3' - Beef_pie    (RM4.00) ");

if (choice==1)
{
//strcpy(choice, "Apple_pie");
price = 2.50 ;
printf("\nYou've selected %d " , choice );
}

else if(choice==2)
{
//strcpy(choice, "Chicken_pie");
price = 4.00;
printf("\nYou've selected %d " , choice );
}

else if (choice==3)
{//strcpy(choice, "Beef_pie");
price = 4.00;
printf("\nYou've selected %d " , choice );
}

else
{
//strcpy(choice, "Invalid");
price=0.00  ;
}

do while  (choice=='c')
{

printf("\n '1' - chocholate_cake   (RM2.50) ");
printf("\n '2' - honey_fruit_cake  (RM4.00) ");
printf("\n '3' - prune_cake        (RM4.00) ");
printf("\n '4' - pisang_cake       (RM4.00) ");

if (choice==1)
{
//strcpy(choice, "chocholate_cake");
price = 2.50 ;
printf("\nYou've selected %d " , choice );
}

else if(choice==2)
{
//strcpy(choice, "honey_fruit_cake");
price = 4.00;
printf("\nYou've selected %d " , choice );
}

else if (choice==3)
{
//strcpy(choice, "prune_cake");
price = 4.00;
printf("\nYou've selected %d " , choice );
}

else if (choice==4)
{ //strcpy(choice, "pisang_cake");
price = 4.00;
printf("\nYou've selected %d " , choice );
}
else
{
//strcpy(choice, "Invalid");
price=0.00  ;
}

scanf("%d" ,&quantity);

total_price = quantity*price;

printf("\nThe total price is %.2f " , total_price);

getche();

}```

2. Your compiler obviously doesn't like the idea of "prune cake".

Seriously, post up the errors you're getting. No sense us repeating the work your compiler has already done.

Did you rem out the code for say, cake, and see if the error was eliminated? Have you done any troubleshooting to fix this?

You're taking us a bit for granted, here.

3. it says - syntax error before '}' token . its at the end of the code.

and im not sure about the while (choice=='p').

any ideas? thx!

4. One thing missing is the closing brace to the do while loop.

Second thing is that main should return an int, and doesn't.

See what that fixes!

I managed to fix it.

Code:
```#include<stdio.h>
#include<string.h>

void main()
{
int quantity ;
float total_price, price;

printf("\n Enter [P] to select Pie        ");
printf("\n Enter [C] to select Pie        \n");
scanf("%c" ,&choice);
fflush(stdin);
if (choice=='p')
{
printf("\n '1' - Apple_pie   (RM2.50) ");
printf("\n '2' - Chicken_pie (RM4.00) ");
scanf(" %c" ,&choice);

if (choice=='1')
{
price = 2.50 ;
}

else if(choice=='2')
{
price = 4.00;
}

else
{
price=0.00;
}
}

else if  (choice=='c')
{
printf("\n '1' - chocholate_cake   (RM2.50) ");
printf("\n '2' - honey_fruit_cake  (RM4.00) ");
printf("\n '3' - prune_cake        (RM4.00) ");
scanf("%c" ,&choice);

if (choice=='1')
{
price = 2.50 ;
}

else if(choice=='2')
{
price = 4.00;
}

else if (choice=='3')
{
price = 4.00;
}

else
{
price=0.00;
}

}

else
printf("\nInvalid");

scanf("%d" ,&quantity);

total_price = quantity*price;

printf("\nThe total price is %.2f " , total_price);

getche();
}```
prune cake anyone?

6. Code:
`     printf("\n Enter [C] to select Pie        \n");`
Pie != Cake, right?

You probably also should take care of the "invalid" choices so as not to print rubbish (menu is not set to anything) and ask for quantity.

I personally would prefer a switch-statement on the choice selection. [But where it me doing this, I'd probably write a function that takes a table of cake/pie names and price, and performs the selection/calculation based on that]

--
Mats