71.0k views
0 votes
In C programming

You are to write a "My HEB2" program which will give the user a menu to shop from and display the receipt at the end.
The choices for the user are:
1. Chicken
2. Fritos
3. Bread
4. Fajitas
5.00
3.49
0.03
7.99
I
5. Purchase Items.
You should allow the user to select as many times as they want (you will use a loop for this).
When the user presses 5, the program will calculate the receipt.
Be aware, there is a coupon which will give you 20% off IF you purchase at least 1 of each item.
In the receipt, show the item, the quantity and the total cost.
Display the total price, The amount saved if they used the coupon, the tax amount and the grand total (which is the total + tax).

User Tnyfst
by
6.8k points

1 Answer

3 votes

Answer:

Here's an example program in C that fulfills the requirements:

#include <stdio.h>

#define CHICKEN_PRICE 5.00

#define FRITOS_PRICE 3.49

#define BREAD_PRICE 0.03

#define FAJITAS_PRICE 7.99

int main() {

int chicken_qty = 0, fritos_qty = 0, bread_qty = 0, fajitas_qty = 0;

char choice;

float subtotal = 0.0, tax = 0.0, total = 0.0, discount = 0.0;

do {

printf("\\*** My HEB2 ***\\");

printf("1. Chicken - $%.2f\\", CHICKEN_PRICE);

printf("2. Fritos - $%.2f\\", FRITOS_PRICE);

printf("3. Bread - $%.2f\\", BREAD_PRICE);

printf("4. Fajitas - $%.2f\\", FAJITAS_PRICE);

printf("5. Purchase Items\\");

printf("Enter choice (1-5): ");

scanf(" %c", &choice);

switch (choice) {

case '1':

chicken_qty++;

subtotal += CHICKEN_PRICE;

printf("Added 1 chicken to cart.\\");

break;

case '2':

fritos_qty++;

subtotal += FRITOS_PRICE;

printf("Added 1 bag of Fritos to cart.\\");

break;

case '3':

bread_qty++;

subtotal += BREAD_PRICE;

printf("Added 1 loaf of bread to cart.\\");

break;

case '4':

fajitas_qty++;

subtotal += FAJITAS_PRICE;

printf("Added 1 fajitas to cart.\\");

break;

case '5':

break;

default:

printf("Invalid choice. Try again.\\");

break;

}

} while (choice != '5');

if (chicken_qty > 0 && fritos_qty > 0 && bread_qty > 0 && fajitas_qty > 0) {

discount = subtotal * 0.2;

}

printf("\\*** Receipt ***\\");

if (chicken_qty > 0) printf("%d Chicken - $%.2f\\", chicken_qty, chicken_qty * CHICKEN_PRICE);

if (fritos_qty > 0) printf("%d Fritos - $%.2f\\", fritos_qty, fritos_qty * FRITOS_PRICE);

if (bread_qty > 0) printf("%d Bread - $%.2f\\", bread_qty, bread_qty * BREAD_PRICE);

if (fajitas_qty > 0) printf("%d Fajitas - $%.2f\\", fajitas_qty, fajitas_qty * FAJITAS_PRICE);

printf("\\Subtotal: $%.2f\\", subtotal);

if (discount > 0.0) {

printf("20%% Discount: -$%.2f\\", discount);

}

tax = subtotal * 0.0825;

printf("Tax: $%.2f\\", tax);

total = subtotal + tax - discount;

printf("Total: $%.2f\\", total);

return 0;

}

Step-by-step explanation:

This program uses a do-while loop to repeatedly prompt the user for input until they choose to purchase their items (option 5). It keeps track of the quantity of each item selected, as well as the subtotal. If the user purchases at least one

User Yathi
by
8.5k points