Thread: where did i go wrong with my codes?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    6

    Exclamation where did i go wrong with my codes?

    is it possible if i put while statement in the switch case to be in order to ask another order?

    Code:
    #include <stdio.h>
    main(){
           int menu;
           int total;
           int price;
           int order;
           int quantity;
           menu:
           printf("\t \t Hello! Welcome to Kathyrn's Bake Shop! \n\n");
           printf("\t \t \t\tMenu list: \n\n");
           printf("(A)Swiss Roll (40.00) \t (B)French Bread (50.00)  (C)Pandesal (2.00)\n");
           printf("(D)Ensaymada(5.00) \t (E)Egg Bread(5.00) \t (F)Chicken Bread(5.00)\n");
           printf("(G)Slice Bread(35.00) \t (H)Monay(4.00) \t (I)pineapple bread (8.00)\n");
           printf("(J)Cheese Roll (5.00) \t (K)Empanada(12.00) \t (L)Pan de Coco(10.00)\n");
           printf("(M)Siopaw(30.00) \t (N)Bake Siopaw(35.00) \t (O)Siopaw Asado(35.00)\n\n");
           printf("\t\t\t Menu List For Cakes and Roll \n\n");
           printf("(1)Birthday Cake small (120.00) \t (2)Birthday Cake Medium(180.00)\n");
           printf("(3)Birthday Cake Large(220.00)\t\t\t(4)Chocolate cake(180.00)\n");
           printf("(5)Ube cake (130.00) \t \t \t\t(6)Pandan Cake(140.00)\n");
           printf("(7)Black Forest Cake(300.00) \t\t\t (8)Chiffon cake (120.00)\n");
           printf("(9)Chocolate Roll(99.00)\t \t \t(P)Ube Roll(99.00)\n\n\n");
           
           printf("Choose Your order:");
           scanf("%s",&order);
           printf("How many would you want to order?:");
           scanf("%d",&quantity);
           
           switch(order){
                        case 'A': case 'a':
                             printf("total = %d",40*quantity);
                             break;
                        case 'B': case 'b':
                             printf("total = %d",50*quantity);
                             break;
                        case 'C': case 'c':
                             printf("total = %d",2*quantity);
                             break;
                        case 'D': case 'd':
                             printf("total = %d",5*quantity);
                             break;
                        case 'E': case 'e':
                             printf("total = %d",5*quantity);
                             break;      
                        case 'F': case 'f':
                             printf("total = %d",5*quantity);
                             break;
                        case 'G': case 'g':
                             printf("total = %d",35*quantity);
                             break;
                        case 'H': case 'h':
                             printf("total = %d",4*quantity);
                             break;
                        case 'I': case 'i':
                             printf("total = %d",8*quantity);
                             break;
                        case 'J': case 'j':
                             printf("total = %d",5*quantity);
                             break;
                        case 'K': case 'k':
                             printf("total = %d",12*quantity);
                             break;
                        case 'L': case 'l':
                             printf("total = %d",10*quantity);
                             break;
                        case 'M': case 'm':
                             printf("total = %d",30*quantity);
                             break;
                        case 'N': case 'n':
                             printf("total = %d",35*quantity);
                             break;
                        case 'O': case 'o':
                             printf("total = %d",35*quantity);
                             break; 
                        case '1':
                             printf("total = %d",120*quantity);
                             break;
                        case '2':
                             printf("total = %d", 180*quantity);
                             break;
                        case '3':
                             printf("total = %d", 220*quantity);
                             break;
                        case '4':
                             printf("total = %d",180*quantity);
                             break;
                        case '5':
                             printf("total = %d", 130*quantity);
                             break;
                        case '6':
                             printf("total = %d", 140*quantity);
                             break;
                        case '7':
                             printf("total = %d", 300*quantity);
                             break;
                        case '8':
                             printf("total = %d", 120*quantity);
                             break;
                        case '9':
                             printf("total = %d", 99*quantity);
                             break;
                        case 'P': case 'p':
                             printf("total = %d", 99*quantity);
                             break;          
                             
                            default:
                                    return 0;
                             }
                        
           
           
           getch();
           }

  2. #2
    spaghetticode
    Guest
    Quote Originally Posted by glendale132 View Post
    is it possible if i put while statement in the switch case to be in order to ask another order?
    The other way round sounds more reasonable - put your switch inside of a loop. There's a lot of redundancy in your code - several cases lead to the exact same calculation. You should sum these up like you already did with uppercase and lowercase letters. Plus you could try to come up with a function based solution - having so many different branches for such a similar task always smells like a bad design decision.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You also need to learn the differences between variable types when using scanf() and in your case statements. Look at this snippet:

    Code:
    ...
           int order;
    ...
           menu:  // What is this for?? Is there a nasty goto somewhere in your code? If so why??
    ...        
           printf("Choose Your order:");
           scanf("%s",&order);
    ...        
           switch(order){
                        case 'A': case 'a':
    First you declare order as in int. Then in your scanf() you treat order like a string. Then in your switch you treat order like a char.

    The scanf() function is very demanding that the format specifiers match the variable type. Not using the correct specifier for the variable type usually invokes undefined behavior.

    Trying to compare an int to a char in your switch statement will probably not produce your desired results. You may want to find and study an ASCII chart to see how the characters are mapped to numeric values.

    Oh and by the way having orders as an int will mean, once you use the correct format specifier with scanf(), will mean that you can't enter the letter 'A' for a menu item because scanf() knows that an int should only accept the 10 digit characters or the +- signs.

    Jim

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    6
    thanks for the advice sir may i ask? can i do this with array? for shorten the code? (sorry for bad english)
    im new to c programming maybe you can help me a bit.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I suggest you first get your current program working properly before you try to add more complexity.

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2015
    Posts
    6
    i think its working with switch case but my problem is "order another dish/bread" im thinking if i use if else statement.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Looks more like some sort of loop to me.

    Jim

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    3
    Change the declaration int order; into char order.
    And then change scanf("%c",&order);

    You will get the expected output. Also if you want to use more order. Then your code should be like this,

    Code:
    do
    {
    
    switch(order)
    {
    .
    .
    .
    .
    }
    
    printf("Would you like to continue the order press 1\n");
    //get the value
    } while(check the value with 1); // If its true it will continue otherwise loop will be exit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate area codes within a list of area codes
    By Staja24 in forum C Programming
    Replies: 9
    Last Post: 05-06-2015, 09:28 PM
  2. Whats wrong with my codes
    By Mustafa Ekicim in forum C Programming
    Replies: 8
    Last Post: 04-10-2014, 11:38 AM
  3. how to add codes ?
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 08-23-2010, 01:33 AM
  4. Key codes...
    By Supar in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2003, 08:48 PM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

Tags for this Thread