Thread: Help with functions

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

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

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    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);
    }
    "without goto we would be wtf'd"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pretty awful.
    - global variables
    - checkChoice() and mainMenu() in a mutually recursive death spiral.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    }
    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: 04-28-2018, 02:02 AM
  2. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  3. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread