I have been set a task to implement arrays into this code to make its usability better could any help and show me how i could do this
Code:
#include <stdio.h>
#include <stdlib.h> // This library is chosen so functions can be used <stdlib.h>
char ItemChosen = 0;
int Orange = 0;
int Apple = 0;
int Pear = 0;
int Budget = 0;
int RemainingBudget = 0;
char ContinueShopping = 0;
void checkChoice();
void mainMenu();
void setPrices(); // This is the function Decleration for all the functions
void getBudget();
void thankYou();
void mainMenu() {
printf("**************************************\n");
printf("Shop Menu : \n");
printf("Item: Price:\n");
printf("O: %d\n", Orange); // function initilization for the Menu
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);
checkChoice();
}
void thankYou() {
printf("Thanks for shopping with us!\n"); // Function intlization for thank you
exit(0);
}
int main() {
setPrices();
getBudget();
mainMenu();
return 0;
}
void checkChoice() { // Function Initilization to Check the choice of the fruit chosen
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;
printf("Remaining budget : %d\n", RemainingBudget);
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);
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);
success = 1;
}
else { success = 0; }
}
else {
printf("Incorrect item (%c) selected.\n", ItemChosen);
thankYou();
}
if (success) {
Budget = RemainingBudget;
printf("Do you want to keep shopping (y/n)");
scanf_s(" %c", &ContinueShopping);
if (ContinueShopping == 'Y') {
mainMenu();
checkChoice(); // function call for both menu and checking the fruit choice
}
else if (ContinueShopping == 'N'); {
thankYou();
}
}
else {
printf("Error your budget has insufficant funds\n");
}
}
void setPrices() {
printf("Please enter the price of Oranges :");
scanf_s(" %d", &Orange);
printf("Please enter the price of Apples :"); // Function initlization for the shop keeper to set the fruit price
scanf_s(" %d", &Apple);
printf("Please enter the price of Pears :");
scanf_s(" %d", &Pear);
}
void getBudget() {
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n"); // function Initlization for the customer to enter their budget
printf("Please enter your budget : ");
scanf_s(" %d", &Budget);
}