Thread: Help for a beginner

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

    Help for a beginner

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int Orange;
        int Apple;
        int Pear;
        int Budget;
        char ItemChosen;
        int RemainingBudget;
    
    
        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, 1);
        
        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");
            }
        }
        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");
            }
        }
        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");
            }
        }
        return 0;
    }
    I need some help to be able to make this code more efficient and instead it just being 'if' statements, how i could reduce it into 'else if' statements. Thank you.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    750
    Since ItemChosen is a single char, you could use a switch - case statement instead of the if statements.

    Also, I would break this single main() function into individual functions.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    8
    Could you show me an example of what it would look like as I'm am fairly new so I'm still getting to grips with everything?

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    126
    I feel you should look at removing repetition.

    Maybe make a "ChosenPrice" variable, set that based on the user's selection, then the three blocks can be merged into

    Code:
            if (ChosenPrice <= 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", ChosenPrice);
                RemainingBudget = Budget - ChosenPrice;
                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");
            }
    And will only need to appear once.

    Also, "insufficant" should be "insufficient"

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    8
    How can I assign a variable to whatever the user selects, I didn't know how to do that bit hence the long and repetitive code. Thank you

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    750
    You should initialize all local variables to zero or some meaningful values.

    You assume whole numbers, you should use floats or doubles for all prices and budgets. Are all fruit whole dollars, or less than a dollar a piece?

    You only allow the purchase of one apple, etc... at a time, You might want to add number of apples to be purchased at a time.

    The user should be able to type 'A', or 'a', for choosing "Apple". Use toupper() to convert 'a', to 'A' for the switch statement.

    I converted your scanf_s() to scanf() for my compiling on Linux. scanf_s() is a non-standard Windows function.

    There are many ways to do this. Prices of fruit could be read from a text file, etc... I added a function to simplify repeated code.

    Based on your code presented, study my version below to start:

    Code:
    #include <stdio.h>
    #include <ctype.h>   // For toupper()
    
    int success(int budget, char *item, int price);
    
    int main(void)
    {
        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("%d", &Orange);
        printf("Please enter the price of Apples : ");
        scanf("%d", &Apple);
        printf("Please enter the price of Pears : ");
        scanf("%d", &Pear);
    
    
        printf("**************************************\n");
        printf("Hello and welcome to my fruit shop\n");
        printf("Please enter your budget : ");
        scanf("%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(" %c", &ItemChosen);
    
        switch(toupper(ItemChosen))
        {
           case 'A' :
              RemainingBudget = success(Budget, "Apple", Apple);
              break;
           case 'P' :
              RemainingBudget = success(Budget, "Pear", Pear);
              break;
           case 'O' :
              RemainingBudget = success(Budget, "Orange", Orange);
              break;
           default:
              printf("Your selection is not available\n");
              RemainingBudget = Budget;
              break;
        }
    
        printf("Remaining budget : %d\n", RemainingBudget);
        printf("Thanks for shopping with us!\n");
    
        return 0;
    }
    
    
    int success(int budget, char *item, int price)
    {
       if(price > budget)
       {
          printf("Error your budget has insufficant funds\n");
       }
       else
       {
          printf("Your purchase has been successful!\n");
          printf("Purchase details\n");
          printf("%s\n", "---------------------------------\n");
          printf("Item selected : %s\n", item);
          printf("Item price : %d\n", price);
          budget -= price;
       }
    
       return budget;
    }

  7. #7
    Registered User
    Join Date
    Oct 2020
    Posts
    8
    Hi , this is very helpful but i am new to programming and have yet to learn about some of the things you have used in your code, i am still getting to know the basics. Could you show me how i would implement my code with just 'if' statements into 'else if' statements to make it more efficient?

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    750
    Understood. Please apply at least some of my suggestions above.

    You need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    My updated code follows:

    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(" %d", &Orange);
        printf("Please enter the price of Apples :");
        scanf(" %d", &Apple);
        printf("Please enter the price of Pears :");
        scanf(" %d", &Pear);
    
    
        printf("**************************************\n");
        printf("Hello and welcome to my fruit shop\n");
        printf("Please enter your budget : ");
        scanf(" %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(" %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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. Beginner...
    By Devil Panther in forum Game Programming
    Replies: 38
    Last Post: 04-20-2004, 08:08 PM
  3. help a beginner
    By psychopath in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2004, 12:47 PM
  4. Help for beginner please!
    By sflyers in forum C++ Programming
    Replies: 12
    Last Post: 01-10-2004, 09:10 PM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

Tags for this Thread