Skip to content

Instantly share code, notes, and snippets.

@cb1kenobi
Created April 27, 2021 14:14
Show Gist options
  • Save cb1kenobi/76f8feb7fb499e031147a2d6c7ed3a3a to your computer and use it in GitHub Desktop.
Save cb1kenobi/76f8feb7fb499e031147a2d6c7ed3a3a to your computer and use it in GitHub Desktop.
Learning C with Jordan
#include <stdio.h>
#include <stdlib.h>
#include "pizza.h"
// Function Prototypes
int size_menu(void);
int top_menu(void);
void clear(void);
void InitStructs(struct PizzaCosts *Costs, struct StuffBought *Totals);
void Order(struct PizzaCosts *Costs, struct StuffBought *Totals);
int PrintReceipt(struct PizzaCosts *Costs, struct StuffBought *t);
int AskAgain(void);
// End of Function Prototypes
void main(void)
{
do {
struct PizzaCosts Cost;
struct StuffBought Total;
struct PizzaCosts *Costs;
struct StuffBought *Totals;
Costs = &Cost;
Totals= &Total;
InitStructs(Costs, Totals);
Order(Costs, Totals);
PrintReceipt(Costs, Totals);
}
while(AskAgain() > 0);
}
void clear(void)
{
system("cls");
}
int size_menu(void)
{
int size;
clear();
printf("\t\t\t\tThe Menu\n\t\t\t\t--------\n\n");
printf("Please choose a size:\n");
printf("[1] Large\n");
printf("[2] Medium\n");
printf("[3] Small\n\n");
printf("Your choice: ");
scanf("%d", &size);
return size;
}
int top_menu(void)
{
int top;
clear();
printf("\t\t\t\tThe Menu\n\t\t\t\t--------\n\n");
printf("Please choose a topping:\n");
printf("[1] Cheese\n");
printf("[2] Pepperoni\n");
printf("[3] Sausage\n");
printf("[4] Special\n\n");
printf("Your choice (0 to stop): ");
scanf("%d", &top);
return top;
}
void Order(struct PizzaCosts *Costs, struct StuffBought *Totals)
{
Totals->Size = size_menu();
if (Totals->Size == 1)
Totals->total += Costs->large;
if (Totals->Size == 2)
Totals->total += Costs->medium;
if (Totals->Size == 3)
Totals->total += Costs->small;
do {
Totals->topping = top_menu();
if (Totals->topping == 1) {
Totals->total += Costs->Cheese;
Totals->NumOfCheese += 1;
}
if (Totals->topping == 2) {
Totals->total += Costs->Pepperoni;
Totals->NumOfPepperoni += 1;
}
if (Totals->topping == 3) {
Totals->total += Costs->Sausage;
Totals->NumOfSausage += 1;
}
if (Totals->topping == 4) {
Totals->total += Costs->Special;
Totals->NumOfSpecial += 1;
}
}
while(Totals->topping !=0);
}
void InitStructs(struct PizzaCosts *Costs, struct StuffBought *Totals) {
Costs->large = 12.00; // cost of items.
Costs->medium = 10.00;
Costs->small = 9.00;
Costs->Cheese = 0.95;
Costs->Pepperoni = 2.95;
Costs->Sausage = 1.95;
Costs->Special = 3.95;
Totals->NumOfCheese=0;
Totals->NumOfPepperoni=0;
Totals->NumOfSausage=0;
Totals->NumOfSpecial=0;
Totals->topping=0;
Totals->Size=0;
Totals->total=0;
}
int PrintReceipt(struct PizzaCosts *Costs, struct StuffBought *t) {
float total;
total = t->total;
clear();
printf("\nYour Receipt:");
printf("\n\nItem\t\tPrice\t\tQty\n----\t\t-----\t\t---\n\n");
if (t->Size == 1)
printf("Large Pizza\t%.2f\t\t1\n", Costs->large);
if (t->Size == 2)
printf("Medium Pizza\t%.2f\t\t1\n", Costs->medium);
if (t->Size == 3)
printf("Small Pizza\t%.2f\t\t1\n", Costs->small);
if (t->NumOfCheese != 0)
printf("Cheese\t\t%.2f\t\t%d\n", Costs->Cheese, t->NumOfCheese);
if (t->NumOfPepperoni != 0)
printf("Pepperoni\t%.2f\t\t%d\n", Costs->Pepperoni, t->NumOfPepperoni);
if (t->NumOfSausage != 0)
printf("Sausage\t\t%.2f\t\t%d\n", Costs->Sausage, t->NumOfSausage);
if (t->NumOfSpecial != 0)
printf("Special\t\t%.2f\t\t%d\n", Costs->Special, t->NumOfSpecial);
printf("\nTotal: %.2f\n\n", total);
return 0;
}
int AskAgain(void) {
char answer;
while (1) {
printf("\nWould You Like to Take Another Order? (Y/n) ");
answer = getchar();
if (answer == 'Y' || answer == 'y')
return 1;
if (answer == '\0' || answer == ' ' || answer == '\n')
return 1;
if (answer == 'n' || answer == 'N')
return 0;
}
}
// Structures
struct PizzaCosts { // This Structure holds the current
float large; // cost of items.
float medium;
float small;
float Cheese;
float Pepperoni;
float Sausage;
float Special;
};
struct StuffBought {
int NumOfCheese; // This structure keeps track of
int NumOfPepperoni; // the number of items we bought
int NumOfSausage; // when it comes time to print the
int NumOfSpecial; // recipt...What's a Special Chris???
int topping;
int Size;
float total;
};
// End of Structues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment