简体   繁体   中英

Need help in looping menu and customer in basic C program

I'm trying to figure out how to create a restaurant menu for my school project, I'm stuck at the looping section for customer1 (still have 2 customers to go), can't figure out what problem it is.

My program will skip combo after the first combo and straight going to quantity. and my tax isn't working too.

Does anybody know how to do it? Here's my program in progress.

#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define COMBOA 8.50
#define COMBOB 10.50
#define COMBOC 18.00
#define COMBOD 32.50
#define SSTAX 0.01

void main()
{
    void logo();
    void menu();
    char cus1();
    char comboA[] = "8.50";
    char comboB[] = "10.50";
    char comboC[] = "18.00";
    char comboD[] = "32.50";
    char member, combo, repeat;
    char error[] = "INVALID_INPUT";

    logo();
    menu();
    cus1();

    system("pause");
}

void logo()
{ //My logo for my restaurant }

void menu()
{
    printf("---------------------------------< MENU >---------------------------------------\n");
    printf("Combo A\n> 1x Burger + 1x Drink\nPrice : RM8.50\n\n");
    printf("Combo B\n> 2x Burger + 2x Drinks + 1x Salad + 1x Nugget\nPrice : RM10.50\n\n");
    printf("Combo C\n> 3x Burger + 3x Drinks + 2x Salad + 1x Nugget\nPrice : RM18.00\n\n");
    printf("Combo D\n> 4x Burger + 4x Drinks + 2x Salad + 2x Nugget + 2x MashPotato\nPrice : RM32.50\n\n");
    printf("--------------------------------------------------------------------------------\n");
}

char cus1()
{
    char combo, repeat,member;
    double purchase, discount, discountRate, total, finaltot, tax, amountReceived, change, profit, combo1;
    int quan, counter;

    printf("Customer No:1\n");

    do
        {
            printf("Please select Combo A/B/C/D (Enter 'X' to exit) : ");
            combo = getchar();
            printf("\nQuantity : ");
            scanf("%d", &quan);

            switch (combo)
            {
            case 'A':
            case 'a':
                combo1 = 8.50;
                break;
            case 'B':
            case 'b':
                combo1 = 10.50;
                break;
            case 'C':
            case 'c':
                combo1 = 18.00;
                break;
            case 'D':
            case 'd':
                combo1 = 32.50;
                break;
            default:
                printf("Invalid input");
            }

            total = (double)quan * combo1;

            printf("\t\tCombo %c : %d @ RM%.2f = RM %.2f\n\n", combo, quan, combo1, total);

            printf("---------------------------------------------------------------\n");
    } while (combo != 'X' && combo != 'x');

        printf("\nIs customer a member? (Y/N) : ");
        scanf("%c", &member);
        rewind(stdin);
        printf("\nEnter amount purchase(RM) :");
        scanf("%f", &purchase);
        rewind(stdin);
        printf("\nEnter amount received from customer(RM) : ");
        scanf("%f", &amountReceived);
        rewind(stdin);

        if (member == 'Y' || member == 'y')
        {
            //Customer is a member
            if (purchase >= 100.00)

                discountRate = 0.30; //30% discount
            else
                discountRate = 0.10; //10% discount
        }
        else
        {
            //Customer is not a member
            if (purchase >= 100.00)

                discountRate = 0.10; //10% discount
            else
                discountRate = 0.00; //No discount
        }

        tax = purchase * SSTAX;
        discount = discountRate * purchase;
        finaltot = purchase - discount - SSTAX;
        change = amountReceived - finaltot;

        printf("COMBO CHARGES = RM%.2f\n", total);
        printf("ADD 10% SST = RM%.2f\n", tax);
        printf("FINAL TOTAL = RM%.2f\n", finaltot);
        printf("AMOUNT RECEIVED = RM%.2f\n", amountReceived);
        printf("CHANGE DUE = RM%.2f\n", change);

        return 0;
}

Add a space before the format code in your scanf() calls.

scanf("%c", &member);
//Becomes
scanf(" %c", &member);

The problems comes from the fact the new line character ( \\n ) is left in the stream so the next scanf() reads it and immediately passes without any user input. Adding a space before a format code prevents that from happening.

Also, using rewind(stdin) is not useful as you can't seek on console streams.

I have made some change in your program see if it works:-

#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define COMBOA 8.50
#define COMBOB 10.50
#define COMBOC 18.00
#define COMBOD 32.50
#define SSTAX 0.01

int main()
{
    void logo();
    void menu();
    char cus1();
    char comboA[] = "8.50";
    char comboB[] = "10.50";
    char comboC[] = "18.00";
    char comboD[] = "32.50";
    char member, combo, repeat;
    char error[] = "INVALID_INPUT";

    logo();
    menu();
    cus1();

    system("pause");
    return 0;
}

void logo()
{ //My logo for my restaurant }
}

void menu()
{
    printf("---------------------------------< MENU >---------------------------------------\n");
    printf("Combo A\n> 1x Burger + 1x Drink\nPrice : RM8.50\n\n");
    printf("Combo B\n> 2x Burger + 2x Drinks + 1x Salad + 1x Nugget\nPrice : RM10.50\n\n");
    printf("Combo C\n> 3x Burger + 3x Drinks + 2x Salad + 1x Nugget\nPrice : RM18.00\n\n");
    printf("Combo D\n> 4x Burger + 4x Drinks + 2x Salad + 2x Nugget + 2x MashPotato\nPrice : RM32.50\n\n");
    printf("--------------------------------------------------------------------------------\n");
}

char cus1()
{
    char combo, repeat,member;
    double purchase, discount, discountRate, total, finaltot, tax, amountReceived, change, profit, combo1;
    int quan, counter;

    printf("Customer No:1\n");

    do
        {
            printf("Please select Combo A/B/C/D (Enter 'X' to exit) : ");
            fflush(stdin);
            combo = getchar();


            if(combo=='X')
                break;
            printf("\nQuantity : ");
            scanf("%d", &quan);

            switch (combo)
            {
            case 'A':
            case 'a':
                combo1 = 8.50;
                break;
            case 'B':
            case 'b':
                combo1 = 10.50;
                break;
            case 'C':
            case 'c':
                combo1 = 18.00;
                break;
            case 'D':
            case 'd':
                combo1 = 32.50;
                break;
            default:
                printf("Invalid input");
            }

            total = (double)quan * combo1;

            printf("\t\tCombo %c : %d @ RM%.2f = RM %.2f\n\n", combo, quan, combo1, total);

            printf("---------------------------------------------------------------\n");
    } while (combo != 'X' && combo != 'x');

        printf("\nIs customer a member? (Y/N) : ");
        scanf("%c", &member);
        rewind(stdin);
        printf("\nEnter amount purchase(RM) :");
        scanf("%f", &purchase);
        rewind(stdin);
        printf("\nEnter amount received from customer(RM) : ");
        scanf("%f", &amountReceived);
        rewind(stdin);

        if (member == 'Y' || member == 'y')
        {
            //Customer is a member
            if (purchase >= 100.00)

                discountRate = 0.30; //30% discount
            else
                discountRate = 0.10; //10% discount
        }
        else
        {
            //Customer is not a member
            if (purchase >= 100.00)

                discountRate = 0.10; //10% discount
            else
                discountRate = 0.00; //No discount
        }

        tax = purchase * SSTAX;
        discount = discountRate * purchase;
        finaltot = purchase - discount - SSTAX;
        change = amountReceived - finaltot;

        printf("COMBO CHARGES = RM%.2f\n", total);
        printf("ADD 10 SST = RM%.2f\n", tax);
        printf("FINAL TOTAL = RM%.2f\n", finaltot);
        printf("AMOUNT RECEIVED = RM%.2f\n", amountReceived);
        printf("CHANGE DUE = RM%.2f\n", change);

        return 0;
}

Please see the statement fflush(stdin); , whenever you try to take character as input then it takes input from the standard input buffer. That is why the statement combo = getchar(); takes character automatically from the buffer skipping you to input quantity. To overcome this problem us fflush(stdin) , which deletes the data from current buffer allowing you to enter character.

Also check your line printf("ADD 10% SST = RM%.2f\\n", tax); , this line contains two % symbol. The % symbol after 10 is incorrect. You cannot print % symbol like this because whenever it will find % symbol inside printf it will treat it as placeholder (like %d or %c etc.). Hence your tax value was not printing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM