Problem on - Selection on which item to purchase

This is a discussion on Problem on - Selection on which item to purchase within the C Programming forums, part of the General Programming Boards category; The question - Write a program that gets user selection on which item to purchase. Display the proper menu for ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    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

    Please enter quantity: 10

    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

    Please enter quantity: 5
    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  ;
           }
                
           
            printf("\nPlease enter quantity ");
            scanf("%d" ,&quantity);
            
            total_price = quantity*price;
            
            printf("\nThe total price is %.2f " , total_price);
            
            getche();
          
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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!

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    thanx Adak.
    I managed to fix it.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
         int quantity ;
         float total_price, price; 
         char choice, menu[20];
         
         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) ");
                printf("\nPlease enter your choice:   ");
                scanf(" %c" ,&choice);
                
                if (choice=='1')
                { 
                     strcpy(menu, "Apple_pie");
                     price = 2.50 ;
                }
           
                else if(choice=='2')
                { 
                     strcpy(menu, "Chicken_pie");
                     price = 4.00;
                }
                
                  else
                {
                     strcpy(menu, "Invalid");
                     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) ");
                printf("\nPlease enter your choice:         ");
                scanf("%c" ,&choice);
                
                if (choice=='1')
                {
                     strcpy(menu, "chocholate_cake");
                     price = 2.50 ;
                }
            
                else if(choice=='2')
                {
                     strcpy(menu, "honey_fruit_cake");
                     price = 4.00;
                }
            
                else if (choice=='3')
                {
                     strcpy(menu, "prune_cake");
                     price = 4.00;
                }    
                
                else
                {
                     strcpy(menu, "Invalid");
                     price=0.00;
                 }
                 
            }    
                   
                 else 
                      printf("\nInvalid");      
            
            printf("\nYou've selected %s ",menu);
            printf("\nPlease enter quantity ");
            scanf("%d" ,&quantity);
            
            total_price = quantity*price;
            
            printf("\nThe total price is %.2f " , total_price);
            
            getche();
      }
    prune cake anyone?
    Last edited by karipap; 04-07-2009 at 07:13 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11:06 PM
  2. Help on project
    By ccomputer in forum C Programming
    Replies: 5
    Last Post: 03-16-2006, 04:57 PM
  3. Problem with the purchase function
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 02-07-2006, 11:03 AM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  5. Replies: 0
    Last Post: 02-05-2002, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21