-
Help with functions
Hi, how could I implement functions into this code to make the readability and usability better? Thank you
Code:
#include <stdio.h>
int main()
{
int Orange = 0;
int Apple = 0;
int Pear = 0;
int Budget = 0;
char ItemChosen = 0;
int RemainingBudget = 0;
printf("Please enter the price of Oranges :");
scanf_s(" %d", &Orange);
printf("Please enter the price of Apples :");
scanf_s(" %d", &Apple);
printf("Please enter the price of Pears :");
scanf_s(" %d", &Pear);
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n");
printf("Please enter your budget : ");
scanf_s(" %d", &Budget);
printf("**************************************\n");
printf("Shop Menu : \n");
printf("Item: Price:\n");
printf("O: %d\n", Orange);
printf("A: %d\n", Apple);
printf("P: %d\n", Pear);
printf("**************************************\n");
printf("Please type what item of fruit you would like to purchase : ");
scanf_s(" %c", &ItemChosen);
if (ItemChosen == 'A') {
if (Apple <= Budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Apple);
RemainingBudget = Budget - Apple;
printf("Remaining budget : %d\n", RemainingBudget);
printf("Thank you for Shopping with us!");
}
else {
printf("Error your budget has insufficant funds or Missing item\n");
printf("Thanks for shopping with us!\n");
}
}
else if (ItemChosen == 'P') {
if (Pear <= Budget)
{
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Pear);
RemainingBudget = Budget - Pear;
printf("Remaining budget : %d\n", RemainingBudget);
printf("Thank you for Shopping with us!");
}
else {
printf("Error your budget has insufficant funds or Missing item\n");
printf("Thanks for shopping with us!\n");
}
}
else if (ItemChosen == 'O') {
if (Orange <= Budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Orange);
RemainingBudget = Budget - Orange;
printf("Remaining budget : %d\n", RemainingBudget);
printf("Thank you for Shopping with us!");
}
else {
printf("Error your budget has insufficant funds or Missing item\n");
printf("Thanks for shopping with us!\n");
}
}
else
{
printf("Incorrect item [%c] selected.\n", ItemChosen);
printf("Thanks for shopping with us!\n");
}
return 0;
}
-
Code:
#include <stdio.h>
#include <stdlib.h>
char ItemChosen = 0;
int Orange = 0, Apple = 0, Pear = 0;
int Budget = 0, RemainingBudget = 0;
void checkChoice();
void mainMenu();
void setPrices();
void getBudget();
void thankYou();
void mainMenu() {
printf("**************************************\n");
printf("Shop Menu : \n");
printf("Item: Price:\n");
printf("O: %d\n", Orange);
printf("A: %d\n", Apple);
printf("P: %d\n", Pear);
printf("**************************************\n");
printf("Please type what item of fruit you would like to purchase : ");
scanf(" %c", &ItemChosen);
checkChoice();
}
void thankYou() {
printf("Thanks for shopping with us!\n");
exit(0);
}
int main() {
setPrices();
getBudget();
mainMenu();
return 0;
}
void checkChoice() {
int success = 0;
if (ItemChosen == 'A') {
if (Apple <= Budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Apple);
RemainingBudget = Budget - Apple;
if (RemainingBudget < 0) RemainingBudget = 0;
printf("Remaining budget : %d\n", RemainingBudget);
//printf("Thank you for Shopping with us!");
success = 1;
}
else { success = 0; }
}
else if (ItemChosen == 'P') {
if (Pear <= Budget)
{
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Pear);
RemainingBudget = Budget - Pear;
printf("Remaining budget : %d\n", RemainingBudget);
//printf("Thank you for Shopping with us!");
success = 1;
} else { success = 0; }
}
else if (ItemChosen == 'O') {
if (Orange <= Budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Orange);
RemainingBudget = Budget - Orange;
printf("Remaining budget : %d\n", RemainingBudget);
//printf("Thank you for Shopping with us!");
success = 1;
} else { success = 0; }
} else {
printf("Incorrect item [%c] selected.\n", ItemChosen);
thankYou();
}
if (success) {
Budget = RemainingBudget;
mainMenu();
} else {
printf("Error your budget has insufficant funds or Missing item\n");
}
}
void setPrices() {
printf("Please enter the price of Oranges :");
scanf(" %d", &Orange);
printf("Please enter the price of Apples :");
scanf(" %d", &Apple);
printf("Please enter the price of Pears :");
scanf(" %d", &Pear);
}
void getBudget() {
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n");
printf("Please enter your budget : ");
scanf(" %d", &Budget);
}
-
Pretty awful.
- global variables
- checkChoice() and mainMenu() in a mutually recursive death spiral.
-
Bored waiting for a build....
Code:
#include <stdio.h>
struct store {
int Orange;
int Apple;
int Pear;
};
struct customer {
int budget;
};
void setupStore(struct store *store) {
printf("Please enter the price of Oranges :");
scanf(" %d", &store->Orange);
printf("Please enter the price of Apples :");
scanf(" %d", &store->Apple);
printf("Please enter the price of Pears :");
scanf(" %d", &store->Pear);
}
void setupCustomer(struct customer *customer) {
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n");
printf("Please enter your budget : ");
scanf(" %d", &customer->budget);
}
void Menu(const struct store *store) {
printf("**************************************\n");
printf("Shop Menu : \n");
printf("Item: Price:\n");
printf("O: %d\n", store->Orange);
printf("A: %d\n", store->Apple);
printf("P: %d\n", store->Pear);
}
char Choice(void) {
char ItemChosen;
printf("**************************************\n");
printf("Please type what item of fruit you would like to purchase : ");
scanf(" %c", &ItemChosen);
return ItemChosen;
}
void purchase(char itemCode, int itemCost, struct customer *customer) {
if (itemCost <= customer->budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", itemCode);
printf("item price : %d\n", itemCost);
customer->budget -= itemCost;
printf("Remaining budget : %d\n", customer->budget);
printf("Thank you for Shopping with us!");
}
else {
printf("Error your budget has insufficant funds or Missing item\n");
printf("Thanks for shopping with us!\n");
}
}
int main() {
struct store store;
struct customer customer;
setupStore(&store);
setupCustomer(&customer);
Menu(&store);
char ItemChosen = Choice();
switch(ItemChosen) {
case 'A':
purchase(ItemChosen,store.Apple,&customer);
break;
case 'P':
purchase(ItemChosen,store.Pear,&customer);
break;
case 'O':
purchase(ItemChosen,store.Orange,&customer);
break;
default:
printf("Incorrect item [%c] selected.\n", ItemChosen);
printf("Thanks for shopping with us!\n");
break;
}
return 0;
}