Thread: help with switch(x)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Unhappy help with switch(x)

    So now, I am making a part of a program that works somewhat like a cash register.
    though I am having troubles with the amount tendered.
    In a way that is the amount being transacted is less than the sum, the program would remove the current input and would ask for new amount
    with an error saying that the amount was not sufficient.

    It would be like this:
    //so the sum is, shall we say 100
    Amount Tendered: 50 //the 50 here would be removed and on the same spot, it would ask for an another amount with the error message.
    Change:

    so instead of
    Amount Tendered: 50
    "Error!"
    Amount Tendered:

    it will just remove the input like this:
    Amount Tendered: //and you'll have to reenter the amount here again.
    "Error!"

    and 2nd,
    I really would like to know how to do the part where, let's say, since the switch is only up to 1-5 and 6+ would be part in the default, I would like to know what codes to use so that when the user will enter any of the numbers above 6 as their input, it'll just go back to the Main Menu where the program would ask the user to pick a value meal.

    Thanks in advance <3

    Code:
    #include<stdio.h>
    main()
    {
       int pass,x;
       float a, b, c, sum, amount;
       clrscr();
    
    gotoxy(31,3);
    printf("CASHIERING SYSTEM\n");
    gotoxy(35,4);
    printf("MAIN MENU\n");
    gotoxy(20,8);
    printf("1. Value Meal 1 (Hotdog, Rice, Milk)");
    gotoxy(20,10);
    printf("2. Value Meal 2 (Chicken, Rice, Softdrink)");
    gotoxy(20,12);
    printf("3. Value Meal 3 (Footlong, Fries, Softdrink)");
    gotoxy(20,14);
    printf("4. Value Meal 4 (Porkchop, Rice, IceTea)");
    gotoxy(20,16);
    printf("5. Exit");
    gotoxy(20,20);
    
    printf("Enter a choice:");
    scanf("%d",&x);
    
    clrscr();
    
    switch(x)
    
    {
    case 1:
    gotoxy(25,6);
    printf("You Selected Value Meal 1\n");
    gotoxy(25,7);
    printf("Please Enter price for each item");
    printf("\nHotdog:");
    scanf("%f",&a);
    printf("\nRice:");
    scanf("%f",&b);
    printf("\nMilk:");
    scanf("%f",&c);
    sum=a+b+c;
    printf("\nTotal Payable: %.2f",sum);
    printf("\n\nAmount Tendered:");
    scanf("%.2f",&amount);
    break;
    case 2:
    gotoxy(25,6);
    printf("You Selected Value Meal 2\n");
    printf("Please Enter price for each item");
    printf("\nChicken:");
    scanf("%f",&a);
    printf("\nRice:");
    scanf("%f",&b);
    printf("\nSoftdrink:");
    scanf("%f",&c);
    sum=a+b+c;
    printf("\nTotal Payable: %.2f",sum);
    printf("\n\nAmount Tendered:");
    scanf("%.2f",&amount);
    break;
    case 3:
    gotoxy(25,6);
    printf("You Selected Value Meal 3\n");
    printf("Please Enter price for each item");
    printf("\nFootlong:");
    scanf("%f",&a);
    printf("\nFries:");
    scanf("%f",&b);
    printf("\nSoftdrink:");
    scanf("%f",&c);
    sum=a+b+c;
    printf("\nTotal Payable: %.2f",sum);
    printf("\n\nAmount Tendered:");
    scanf("%.2f",&amount);
    break;
    case 4:
    gotoxy(25,6);
    printf("You Selected Value Meal 4\n");
    printf("Please Enter price for each item");
    printf("\nPorkchop:");
    scanf("%f",&a);
    printf("\nRice:");
    scanf("%f",&b);
    printf("\nIcetea:");
    scanf("%f",&c);
    sum=a+b+c;
    printf("\nTotal Payable: %.2f",sum);
    printf("\n\nAmount Tendered:");
    scanf("%.2f",&amount);
    break;
    case 5:
    exit();
    default:
    gotoxy(25,6);
    printf("OUT OF RANGE!");
    }
    getch();
    }
    also,
    you noticed that in each case, I placed
    sum=a+b+c;
    printf("\nTotal Payable: %.2f",sum);
    printf("\n\nAmount Tendered:");
    scanf("%.2f",&amount);
    on each of them, I was wondering if there is a shorter way? Like Function Call?
    coz I'm really having a hard time with calling a function, it would always go out as "declaration error" Dx

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A lot of your programming questions would be easily solved by just looking at your code

    << IF >>


    You would indent your code. Just 2 to 5 spaces for all the subordinate lines of code - it makes studying your code, or having others do it, so much easier.

    Although you don't need to, because you have a smart compiler/linker like Turbo C, it's still a good idea to explicitly include conio.h, if you are going to include it's functions (like gotoxy()).


    Anytime you see yourself repeating code, you could probably profit by making that code, into a function, or moving it to a more dominant position in your program. Again, it's easy to see these things, once you indent the code.

    for example, if you're asking questions about the total, inside each case of a switch statement, then maybe move the code up above the switch statement, or maybe have that code after the switch statement. Or put it into a function like get_total(), and call it from each case statements inside the switch statement.

    You can't (and shouldn't) use this as a "rule", but generally, repeating code indicates the wrong logic design of the program. The more repetition is needed, the more sure you can be that some refinement of the design, is likely needed.

    To make your queries print out just where you want them to, you can either count the rows from the top (the y), and get the char's over from the left side (the x), and use gotoxy(x,y), or you can use getx() and gety(), to get your coordinates. Somehow, you have to get those coordinates, however.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Add return type to main. Make main return said type.
    Indent. Add some spacing now and then to separate the lines to make it more readable.
    After having done said modifications and taking into account Adak's feedback, post your new code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Sorry it took awhile xP
    I am really having a hard time XD

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
     int x;
     float a,b,c,total,amount,change;
    
    do{
        clrscr();
        gotoxy(31,3);
        printf("CASHIERING SYSTEM\n");
        gotoxy(35,4);
        printf("MAIN MENU\n");
        gotoxy(20,8);
        printf("1. Value Meal 1 (Hotdog, Rice, Milk)");
        gotoxy(20,10);
        printf("2. Value Meal 2 (Chicken, Rice, Softdrink)");
        gotoxy(20,12);
        printf("3. Value Meal 3 (Footlong, Fries, Softdrink)");
        gotoxy(20,14);
        printf("4. Value Meal 4 (Porkchop, Rice, IceTea)");
        gotoxy(20,16);
        printf("5. Exit");
    
        gotoxy(20,19);
        printf("PLEASE ENTER A CHOICE: ");
        scanf("%d",&x);
    
        clrscr();
    
    
    switch (x)
        {
    case 1:
        gotoxy(25,6);
        printf("You Selected Value Meal 1 (Hotdog, Rice, Milk)\n");
        gotoxy(25,7);
        printf("Please Enter price for each item");
        printf("\nHotdog:");
        scanf("%f",&a);
        printf("\nRice:");
        scanf("%f",&b);
        printf("\nMilk:");
        scanf("%f",&c);
    
            total=a+b+c;
            printf("\nTotal payable: %.2f", total);
    
            do{
                printf("\nAmount Tendered: ");
                scanf("%f",&amount);
                if(total>amount)
                    printf("Tendered Amount is not enough!\n");
    
            }while(amount<total);
            change=amount-total;
            printf("\nChange: %.2f", change);
            break;
    
    case 2:
        gotoxy(25,6);
        printf("You Selected Value Meal 2 (Chicken, Rice, Softdrink)\n");
        printf("Please Enter price for each item");
        printf("\nChicken:");
        scanf("%f",&a);
        printf("\nRice:");
        scanf("%f",&b);
        printf("\nSoftdrink:");
        scanf("%f",&c);
    
            total=a+b+c;
            printf("\nTotal payable: %.2f", total);
    
            do{
                printf("\nAmount Tendered: ");
                scanf("%f",&amount);
                if(total>amount)
                    printf("Tendered Amount is not enough!\n");
    
            }while(amount<total);
            change=amount-total;
            printf("\nChange: %.2f", change);
            break;
    
    case 3:
        gotoxy(25,6);
        printf("You Selected Value Meal 3 (Footlong, Fries, Softdrink)\n");
        printf("Please Enter price for each item");
        printf("\nFootlong:");
        scanf("%f",&a);
        printf("\nFries:");
        scanf("%f",&b);
        printf("\nSoftdrink:");
        scanf("%f",&c);
    
            total=a+b+c;
            printf("\nTotal payable: %.2f", total);
    
            do{
                printf("\nAmount Tendered: ");
                scanf("%f",&amount);
                if(total>amount)
                    printf("CTendered Amount is not enough!\n");
    
            }while(amount<total);
            change=amount-total;
            printf("\nChange: %.2f", change);
            break;
    
    case 4:
        gotoxy(25,6);
        printf("You Selected Value Meal 4 (Porkchop, Rice, IceTea)\n");
        printf("Please Enter price for each item");
        printf("\nPorkchop:");
        scanf("%f",&a);
        printf("\nRice:");
        scanf("%f",&b);
        printf("\nIcetea:");
        scanf("%f",&c);
    
            total=a+b+c;
            printf("\nTotal payable: %.2f", total);
    
            do{
                printf("\nAmount Tendered: ");
                scanf("%f",&amount);
                if(total>amount)
                    printf("Tendered Amount is not enough!\n");
    
            }while(amount<total);
            change=amount-total;
            printf("\nChange: %.2f", change);
            break;
    
        case 5:
            exit();
    
        default:
            printf("OUT OF RANGE!"); /--this here will not show unless I type 55(diff no.)5ect...--/
            break;
        }
     }
        while(x>5);
    
     getch();
    }
    And as for the repeated codes, I'm sorry but I'm not sure how to do a return for it or call it so that in a way, I won't have to repeat my codes. I'm very much new to this so I'm not catching up well D;

Popular pages Recent additions subscribe to a feed