Thread: help With arrays

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    16

    help With arrays

    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);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,538
    It's a shame you copied from the WORST possible example.
    Help with functions
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,538
    > I have been set a task to implement arrays into this code
    And arrays of what?

    Arrays for what the shop sells?
    Arrays for the customers?
    Arrays for the shops themselves?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    16
    arrays for the items and for the prices of items.
    The items stored in an array defined as itemPrefixes
    The prices stored in ab integer array as itemPrices.
    The position “i” in itemPrefixes and itemPrice array binds a particular item with its price, e.g.itemPrefixes[0] = ‘A’ is binded with itemPrices[0] = 2.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    16
    Sorry did you have a better solution I must of missed it. was it posted on my last thread?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,538
    Quote Originally Posted by Harveyh View Post
    Sorry did you have a better solution I must of missed it. was it posted on my last thread?
    Help with functions
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2020
    Posts
    16
    Yes i see your solution now, could you show me how i could implement arrays for the shop item and for the prices of these items into that solution?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,538
    Start with changing
    Code:
    struct store {
      int Orange;
      int Apple;
      int Pear;
    };
    to
    Code:
    struct item {
      const char *name;
      int price;
    };
    then in main
    Code:
    struct item store[] = {
      { "Orange", 0 },
      { "Apple", 0 },
      { "Pear", 0 },
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2020
    Posts
    16
    I am having trouble getting arrays to work how can i implement them into this, i jsut need arrays for the item and prices im struggled to get anything to work
    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;
    }

  10. #10
    Registered User
    Join Date
    Oct 2020
    Posts
    16
    struggling*

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,538
    Maybe you should show what you tried.

    Just reposting the same code with zero attempt at putting arrays into it doesn't tell us anything about what is confusing you.

    Create a separate side project which just has the very basic information for a store. No customers, no choosing what to buy.
    Just a store and a list of what's for sale.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-06-2014, 02:39 PM
  2. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread