Thread: Switch case

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    3

    Switch case

    Hello

    My first post here on this board and I would need some help/suggestion

    How can I get the total price of 2 orders ? For example, if I choose cheesburger and sprite ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
    system("COLOR F4");
    
    
    int choixMenu;
    float a = 3.75;
    float b = 4.2;
    float c = 2.1;
    float d = 5.2;
    float e = 3.79;
    
    
    printf("--- Menu ---\n\n");
    printf("1. Burger\n");
    printf("2. Kebab\n");
    printf("3. Cheeseburger\n");
    printf("4. Big Mac\n");
    printf("5. Fish burger\n");
    printf("\n Your choice ? ");
    scanf("%d", &choixMenu);
    printf("\n");
    
    
    system("cls");
    switch (choixMenu)
    {
    case 1:
    printf("You have chosen Burger, the price is %f \n", a);
    break;
    case 2:
    printf("You have chosen Kebab, the price is %f \n", b);
    break;
    case 3:
    printf("You have chosen Cheeseburger, the price is %f !\n", c);
    break;
    case 4:
    printf("You have chosen Big Mac, the price is %f !\n", d);
    break;
    case 5:
    printf("You have chosen Fish Burger, the price is %f \n\n", e);
    default:
    printf("You have chosen wrong number!");
    break;
    }
    
    
    printf("\n What would you like to drink ?\n");
    
    
    int choixMenu1;
    float f = 1.75;
    float g = 2.2;
    float h = 1.9;
    float i = 1.75;
    float j = 2.7;
    
    
    printf("=== Menu Drink ===\n\n");
    printf("6. Sprite\n");
    printf("7. CocaCola\n");
    printf("8. Tea\n");
    printf("9. Water\n");
    printf("10. Bier\n");
    printf("\nYour choice ? ");
    scanf("%d", &choixMenu1);
    printf("\n");
    
    
    switch (choixMenu1)
    {
    case 6:
    printf("You have chosen Sprite, the price for Sprite is  %f euro\n", f);
    break;
    case 7:
    printf("You have chosen CocaCola, the price is %f euro\n", g);
    break;
    case 8:
    printf("You have chosen Tea, the price is %f euro\n", h);
    break;
    case 9:
    printf("You have chosen water, the price is %f euro  !\n", i);
    break;
    case 10:
    printf("You have chosen bier, the price is %f euro \n\n", j);
    default:
    printf("You sellected wrong number !");
    break;
    
    }
    
    
    
    
    return 0;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    I guess you start with something like

    float totalCost = 0.0;

    Then after every printf (and before the break) you do something like
    totalCost += a; // or b,c,d,e....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    I got error

    ||=== Build: Release in mcdo menu (compiler: GNU GCC Compiler) ===|


    C:\aj\Language C\mcu\main.c|32|error: expected identifier or '(' before '+=' token|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    Salem, thank you for your answer. I spent some time with this and now I understand much better.

    First, I had to declare variable 'sum' by
    Code:
    float sum;
    why somebodys write

    Code:
    float sum = 0;
    ?
    Is it big difference or important if I set starting value at 0 ?

    Than, after first ; in each cases, I used
    Code:
    sum += a;
    with correct use of float variables names(a, b, c, d, e, f, g, h, i, j)

    Finally, at the end of second switch case, after }, I wrote

    Code:
    printf("Total sum is %f", sum);


    As far I understand, by declaring first a variable 'sum', than every float value corresponding to specific choice from afterwards switch case choice is added to the value(which is 0 in the beginning) of 'sum' variable. Quite simple, actually, now I just have to make whole program a little bit more nicely looking I am really happy to solved this by trying and spending time,rather than to just copy paste the code.


    With a little bit thinking about whole mini project, this can be also done by just using scanf and printf functions if I previously did while menu with printf function, and than demand user input and with printf calculate total, am I right ?

    Thank you again for help.
    Last edited by mishmash; 11-13-2016 at 04:39 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    Good
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-09-2016, 03:58 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. Switch and Case
    By ednnd in forum C Programming
    Replies: 12
    Last Post: 10-07-2005, 09:46 PM
  4. switch, case help
    By campermama in forum C++ Programming
    Replies: 7
    Last Post: 06-16-2004, 09:49 AM
  5. Switch case help ?
    By Black&White in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2004, 12:04 PM

Tags for this Thread