Home > OS >  How to display the all the input string from if else statement?
How to display the all the input string from if else statement?

Time:12-30

At the end of the program before stating the total cost, I would like the program to list the Item chosen by the user.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct menuDetails {
    double total, priceA, priceB, priceC, priceD, priceE, priceF;
} md;

int main()
{
    int itemTotal, itemName, itemAll;
    int sum=0;
    char item;
    char itemA[10]= "Nasi Ayam";
    char itemB[20]= "Nasi Ayam Merah";
    char itemC[15]= "Nasi Kerabu";
    char itemD[10]= "Sotong";
    char itemE[10]= "Teh Peng";
    char itemF[5]= "Kopi";

    printf("\t========= Menu =========\n");
    printf("Item \t Name \t\t\t Price (RM)\n");
    printf("A \t Nasi Ayam \t\t 3.50\n");
    printf("B \t Nasi Ayam Merah \t 4.00\n");
    printf("C \t Nasi Kerabu \t\t 5.50\n");
    printf("D \t Sotong \t\t 2.00\n");
    printf("E \t Teh Peng \t\t 2.00\n");
    printf("F \t Kopi \t\t\t 2.00\n");
    
    printf("\nHow many items would you like to purchase: ");
    scanf("%d", &itemTotal);
    printf("You have chose to order %d items\n\n", itemTotal);
    
    for(itemName = 1; itemName <= itemTotal; itemName  )
    {
        printf("Item %d [Enter Item (A-F)]: ", itemName);
        scanf(" %c", &item);
        
        md.priceA = 3.50;
        md.priceB = 4.00;
        md.priceC = 5.50;
        md.priceD = 2.00;
        md.priceE = 2.00;
        md.priceF = 2.00;
        
        if (item == 'A' || item == 'a') {
            printf("%s = RM%.2f\n\n", itemA, md.priceA);
            md.total = md.total   md.priceA;
        }else if (item == 'B' || item == 'b') {
            printf("%s = RM%.2f\n\n", itemB, md.priceB);
            md.total = md.total   md.priceB;
        }else if (item == 'C' || item == 'c') {
            printf("%s = RM%.2f\n\n", itemC, md.priceC);
            md.total = md.total   md.priceC;
        }else if (item == 'D' || item == 'd') {
            printf("%s = RM%.2f\n\n", itemD, md.priceD);
            md.total = md.total   md.priceD;
        }else if (item == 'E' || item == 'e') {
            printf("%s = RM%.2f\n\n", itemE, md.priceE);
            md.total = md.total   md.priceE;
        }else if (item == 'F' || item == 'f') {
            printf("%s = RM%.2f\n\n", itemF, md.priceF);
            md.total = md.total   md.priceF;            
        }
    } 
    printf("The total is RM%.2f", md.total);
    
    time_t my_time = time(NULL);
    printf("\nDate: %s", ctime(&my_time));
    
    return 0;
}

The output:

        ========= Menu =========
Item     Name                    Price (RM)
A        Nasi Ayam               3.50
B        Nasi Ayam Merah         4.00
C        Nasi Kerabu             5.50
D        Sotong                  2.00
E        Teh Peng                2.00
F        Kopi                    2.00

How many items would you like to purchase: 3
You have chose to order 3 items

Item 1 [Enter Item (A-F)]: a
Nasi Ayam = RM3.50

Item 2 [Enter Item (A-F)]: b
Nasi Ayam Merah = RM4.00

Item 3 [Enter Item (A-F)]: c
Nasi Kerabu = RM5.50

The total is RM13.00
Date: Thu Dec 29 17:02:49 2022

Expected output:

        ========= Menu =========
Item     Name                    Price (RM)
A        Nasi Ayam               3.50
B        Nasi Ayam Merah         4.00
C        Nasi Kerabu             5.50
D        Sotong                  2.00
E        Teh Peng                2.00
F        Kopi                    2.00

How many items would you like to purchase: 3
You have chose to order 3 items

Item 1 [Enter Item (A-F)]: a
Nasi Ayam = RM3.50

Item 2 [Enter Item (A-F)]: b
Nasi Ayam Merah = RM4.00

Item 3 [Enter Item (A-F)]: c
Nasi Kerabu = RM5.50

Receipt:
Nasi Ayam
Nasi Ayam Merah
Nasi Kerabu

The total is RM13.00
Date: Thu Dec 29 17:02:49 2022

I want to display the list of item chosen by the user.

CodePudding user response:

You could do this by adding a couple more arrays, and creating a for loop to print out the reciept.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_ITEMS 10 

struct menuDetails {
    double total, priceA, priceB, priceC, priceD, priceE, priceF;
} md;

int main()
{
    int itemTotal, itemName, itemAll;
    int sum=0; // you can remove this, it is unused
    char item;

    char itemA[10]= "Nasi Ayam";
    char itemB[20]= "Nasi Ayam Merah";
    char itemC[15]= "Nasi Kerabu";
    char itemD[10]= "Sotong";
    char itemE[10]= "Teh Peng";
    char itemF[5]= "Kopi";

    // arrays to store the chosen items and their quantities
    char chosenItems[MAX_ITEMS];
    int quantities[MAX_ITEMS];
    int numChosenItems = 0;

    printf("\t========= Menu =========\n");
    printf("Item \t Name \t\t\t Price (RM)\n");
    printf("A \t Nasi Ayam \t\t 3.50\n");
    printf("B \t Nasi Ayam Merah \t 4.00\n");
    printf("C \t Nasi Kerabu \t\t 5.50\n");
    printf("D \t Sotong \t\t 2.00\n");
    printf("E \t Teh Peng \t\t 2.00\n");
    printf("F \t Kopi \t\t\t 2.00\n");

    printf("\nHow many items would you like to purchase: ");
    scanf("%d", &itemTotal);
    printf("You have chose to order %d items\n\n", itemTotal);

    for(itemName = 1; itemName <= itemTotal; itemName  )
    {
        printf("Item %d [Enter Item (A-F)]: ", itemName);
        scanf(" %c", &item);
    
        md.priceA = 3.50;
        md.priceB = 4.00;
        md.priceC = 5.50;
        md.priceD = 2.00;
        md.priceE = 2.00;
        md.priceF = 2.00;
    
        // store chosen item and the quantity in array
        if (item == 'A' || item == 'a') {
            printf("%s = RM%.2f\n\n", itemA, md.priceA);
            md.total = md.total   md.priceA;
            chosenItems[numChosenItems] = 'A';
            quantities[numChosenItems] = 1;
            numChosenItems  ;
                }else if (item == 'B' || item == 'b') {
            printf("%s = RM%.2f\n\n", itemB, md.priceB);
            md.total = md.total   md.priceB;
            chosenItems[numChosenItems] = 'B';
            quantities[numChosenItems] = 1;
            numChosenItems  ;
        }else if (item == 'C' || item == 'c') {
            printf("%s = RM%.2f\n\n", itemC, md.priceC);
            md.total = md.total   md.priceC;
            chosenItems[numChosenItems] = 'C';
            quantities[numChosenItems] = 1;
            numChosenItems  ;
        }else if (item == 'D' || item == 'd') {
            printf("%s = RM%.2f\n\n", itemD, md.priceD);
            md.total = md.total   md.priceD;
            chosenItems[numChosenItems] = 'D';
            quantities[numChosenItems] = 1;
            numChosenItems  ;
        }else if (item == 'E' || item == 'e') {
            printf("%s = RM%.2f\n\n", itemE, md.priceE);
            md.total = md.total   md.priceE;
            chosenItems[numChosenItems] = 'E';
            quantities[numChosenItems] = 1;
            numChosenItems  ;
        }else if (item == 'F' || item == 'f') {
            printf("%s = RM%.2f\n\n", itemF, md.priceF);
            md.total = md.total   md.priceF;
            chosenItems[numChosenItems] = 'F';
            quantities[numChosenItems] = 1;
            numChosenItems  ;            
        }
    } 
    printf("The total is RM%.2f\n\n", md.total);

    // Print out the receipt
    printf("===== Receipt =====\n");
    for (int i = 0; i < numChosenItems; i  ) {
        if (chosenItems[i] == 'A') {
            printf("%d x %s\n", quantities[i], itemA, md.priceA);
        } else if (chosenItems[i] == 'B') {
            printf("%d x %s\n", quantities[i], itemB, md.priceB);
        } else if (chosenItems[i] == 'C') {
            printf("%d x %s\n", quantities[i], itemC, md.priceC);
        } else if (chosenItems[i] == 'D') {
            printf("%d x %s\n", quantities[i], itemD, md.priceD);
        } else if (chosenItems[i] == 'E') {
            printf("%d x %s\n", quantities[i], itemE, md.priceE);
        } else if (chosenItems[i] == 'F') {
            printf("%d x %s\n", quantities[i], itemF, md.priceF);
        }
    }
    printf("Total \t\t\t RM%.2f\n", md.total);

    time_t my_time = time(NULL);
    printf("Date: %s", ctime(&my_time));

    return 0;
}

Output:

        ========= Menu =========
Item     Name                    Price (RM)
A        Nasi Ayam               3.50
B        Nasi Ayam Merah         4.00
C        Nasi Kerabu             5.50
D        Sotong                  2.00
E        Teh Peng                2.00
F        Kopi                    2.00

How many items would you like to purchase: 3
You have chose to order 3 items

Item 1 [Enter Item (A-F)]: a
Nasi Ayam = RM3.50

Item 2 [Enter Item (A-F)]: b
Nasi Ayam Merah = RM4.00

Item 3 [Enter Item (A-F)]: c
Nasi Kerabu = RM5.50

The total is RM13.00

===== Receipt =====
1 x Nasi Ayam
1 x Nasi Ayam Merah
1 x Nasi Kerabu
Total                    RM13.00
Date: Thu Dec 29 11:11:24 2022

CodePudding user response:

If you want to list the items stored by the user, and want to allow the user to order the same thing more than once, then one thing you could do is to add an extra counter variable for every menu item. Or even better, make an array, which I call item_counters in the code below:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

struct menu_item
{
    const char *name;
    double price;
};

#define NUM_MENU_ITEMS 6

static const struct menu_item menu_items[NUM_MENU_ITEMS] = {
    { "Nasi Ayam", 3.50 },
    { "Nasi Ayam Merah", 4.00 },
    { "Nasi Kerabu", 5.50 },
    { "Sotong", 2.00 },
    { "Teh Peng", 2.00 },
    { "Kopi", 2.00 }
};

int main( void )
{
    int total_items;
    int item_counters[NUM_MENU_ITEMS] = {0};
    double total_cost = 0.0;

    //print the menu using a loop
    printf("\t========= Menu =========\n");
    printf("Item     Name                   Price (RM)\n");
    for ( int i = 0; i < NUM_MENU_ITEMS; i   )
    {
        printf(
            "%-8c %-22s %lf\n",
            'A' i, menu_items[i].name, menu_items[i].price
        );
    }
    
    printf( "\nHow many items would you like to purchase: ");
    if ( scanf( "%d", &total_items ) != 1 )
    {
        fprintf( stderr, "Input error!\n" );
        exit( EXIT_FAILURE );
    }

    printf( "You have chosen to order %d items.\n\n", total_items );

    //ask user to select items
    for( int i = 0; i < total_items; i   )
    {
        char item;

        //prompt user to select item
        printf( "Item %d [Enter Item (A-%c)]: ", i, 'A' (NUM_MENU_ITEMS-1) );

        //attempt to read one character
        if ( scanf( " %c", &item ) != 1 )
        {
            fprintf( stderr, "Input error!\n" );
            exit( EXIT_FAILURE );
        }

        //make input upper-case, if it is lower-case
        item = toupper( (unsigned char)item );

        //verify that character is valid
        if ( item < 'A' || item > 'A'   (NUM_MENU_ITEMS-1) )
        {
            printf( "Invalid input!\n" );
            exit( EXIT_FAILURE );
        }

        //increment counter for selected item
        item_counters[item-'A']  ;
    }

    //print back the order to the user, and calculate total cost
    printf( "\nYou ordered:\n" );
    for ( int i = 0; i < NUM_MENU_ITEMS; i   )
    {
        if ( item_counters[i] != 0 )
        {
            double total_cost_for_item;

            total_cost_for_item = item_counters[i] * menu_items[i].price;

            printf(
                "%d %s for %lf\n",
                item_counters[i], menu_items[i].name, total_cost_for_item
            );

            total_cost  = total_cost_for_item;
        }
    }

    //print total cost
    printf( "\nThe total is RM%.2f\n", total_cost );

    //print time
    time_t my_time = time(NULL);
    printf( "Date: %s\n", ctime(&my_time));
    
    return 0;
}

This program has the following behavior:

        ========= Menu =========
Item     Name                   Price (RM)
A        Nasi Ayam              3.500000
B        Nasi Ayam Merah        4.000000
C        Nasi Kerabu            5.500000
D        Sotong                 2.000000
E        Teh Peng               2.000000
F        Kopi                   2.000000

How many items would you like to purchase: 5
You have chosen to order 5 items.

Item 0 [Enter Item (A-F)]: a
Item 1 [Enter Item (A-F)]: b
Item 2 [Enter Item (A-F)]: c
Item 3 [Enter Item (A-F)]: a
Item 4 [Enter Item (A-F)]: d

You ordered:
2 Nasi Ayam for 7.000000
1 Nasi Ayam Merah for 4.000000
1 Nasi Kerabu for 5.500000
1 Sotong for 2.000000

The total is RM18.50
Date: Thu Dec 29 15:54:04 2022

Note that I have modified your program in such a way that you can easily change the number of items in the menu, and the program will automatically adapt. For example, I can simply change

#define NUM_MENU_ITEMS 6

to

#define NUM_MENU_ITEMS 7

and add one item to the list:

static const struct menu_item menu_items[NUM_MENU_ITEMS] = {
    { "Nasi Ayam", 3.50 },
    { "Nasi Ayam Merah", 4.00 },
    { "Nasi Kerabu", 5.50 },
    { "Sotong", 2.00 },
    { "Teh Peng", 2.00 },
    { "Kopi", 2.00 },
    { "Cheese Sandwich", 3.00 }
};

As you can see, the program will adapt itself automatically:

        ========= Menu =========
Item     Name                   Price (RM)
A        Nasi Ayam              3.500000
B        Nasi Ayam Merah        4.000000
C        Nasi Kerabu            5.500000
D        Sotong                 2.000000
E        Teh Peng               2.000000
F        Kopi                   2.000000
G        Cheese Sandwich        3.000000

How many items would you like to purchase: 5
You have chosen to order 5 items.

Item 0 [Enter Item (A-G)]: A
Item 1 [Enter Item (A-G)]: G
Item 2 [Enter Item (A-G)]: C
Item 3 [Enter Item (A-G)]: G
Item 4 [Enter Item (A-G)]: F

You ordered:
1 Nasi Ayam for 3.500000
1 Nasi Kerabu for 5.500000
1 Kopi for 2.000000
2 Cheese Sandwich for 6.000000

The total is RM17.00
Date: Thu Dec 29 16:00:03 2022

The menu output was automatically adapted and it now asks and accepts input from A to G instead of from A to F. That is the advantage of not hard-coding anything in your program, except in one place.

It is worth noting that this program assumes that the character codes of the alphabet are stored consecutively in the character set, which is the case for most character sets, including the ASCII character set. However, this may not be the case on some more exotic platforms. On those platforms, the code would not work.

  •  Tags:  
  • c
  • Related