Thread: help with code

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

    help with code

    I have written this code but when debugging an error appears in the function purchaseItem I can't see how to fix it can anyone help. Also any other errors in the code let me know.
    Code:
    #include <stdio.h>
    
    
    int isItemExists(char c, char itemPrefixes[]);
    void displayMenu(char itemPrefixes[], int prices[]);
    int getPrices(char c, char itemPrefixes[], int prices[]);
    void getBudget();
    int purchaseItem(int budget, char itemPurchased, char itemPrefixes[], int prices[]);
    
    
    int size;
    int Budget = 0;
    int RemainingBudget = 0;
    
    
    
    
    int main()
    {
        printf("--- ALL AVAILABLE FRUITS ---\n");
        printf("P: Pears\n");
        printf("M: Mango\n");
        printf("A: Apple\n");
        printf("O: Orange\n");
        printf("C: Cherry\n");
        printf("W: Watermelon\n");
    
    
        printf("-------------------------------------\n");
    
    
        printf("How many Fruits have you got in stock? ");
        scanf_s("%d", &size);
    
    
        int prices[8];
        char itemPrefixes[8];
        int marked = 0;
        while (marked < size) {
            printf("\n(%d) enter Fruit prefix:", marked + 1);
            char prefix = ' ';
            scanf_s(" %c", &prefix, 1);
            if (isItemExists(prefix, itemPrefixes) == 1) {
                printf("Error: Fruit already chosen!");
                continue;
            }
            else {
                itemPrefixes[marked] = prefix;
    
    
                printf("enter Price for (%c):", prefix);
                scanf_s("%d", &prices[marked]);
                marked++;
            }
        }
    
    
        displayMenu(itemPrefixes, prices);
        getBudget();
        purchaseItem(itemPrefixes, prices);
    
    
    
    
            char run = 'y';
        while (run == 'y') {
            char choice;
            printf("\nSHOP OPERATIONS\n");
            printf("1. Enter 'c' to check if Fruit is in stock\n");
            printf("2. Enter 'd' to display Menu\n");
            printf("3. Enter 'm' to display item prices\n");
            printf("4. Enter 'i' to compare budget with item prices\n");
            printf("5. Enter 'p' to purchase an item\n");
            printf("Enter your choice:");
    
    
            scanf_s(" %c", &choice, 1);
    
    
            char cSearch;
            if (choice == 'c') {
                printf("Please enter Fruit prefix to search:");
                scanf_s(" %c", &cSearch, 1);
                int found = isItemExists(cSearch, itemPrefixes);
    
    
                if (found) {
                    printf("Fruit FOUND!");
                }
                else {
                    printf("Fruit NOT FOUND!");
                }
            }
            else if (choice == 'd') {
    
    
                displayMenu(itemPrefixes, prices);
    
    
            }
            else if (choice == 'm') {
                printf("Please enter fruit prefix:");
                scanf_s(" %c", &cSearch, 1);
                int m = getPrices(cSearch, itemPrefixes, prices);
                if (m > 0) {
                    printf("The price of %c is %d:", cSearch, m);
                }
                else {
                    printf("%c has not been selected!", cSearch);
                }
    
    
            }
            else if (choice == 'i') {
                printf("Your avalible budget is %d\n", Budget);
                printf("Please enter an item you would like to comapre your budget with: \n");
                scanf_s(" %c", &cSearch, 1);
                int i = getPrices(cSearch, itemPrefixes, prices);
                if (i <= Budget) {
                    printf("You have sufficiant funds to purchase this fruit\n");
                    RemainingBudget = Budget - i;
                    printf("Remaining budget is: %d\n", RemainingBudget);
                }
                else {
                    printf("You do not have enough funds to purchase this item, Please add funds");
                }
            }
            else if (choice == 'p') {
                displayMenu;
                printf("Please enter what item you would like to purchase from the Menu: ");
                scanf_s("%d", &cSearch);
                int p = purchaseItem(cSearch, itemPrefixes);
                RemainingBudget = Budget - p;
                if (p > Budget) {
                    printf("You have insuffciant funds for this item\n");
                }
                else (p <= Budget); {
                    purchaseItem;
                }
            }
            else {
                printf("Please enter a valid choice!!!");
            }
    
    
            printf("\nDo you want to keep shopping: (y/n)?");
    
    
            scanf_s(" %c", &run, 1);
        }
    
    
        return 0;
    }
    
    
    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);
    }
    int isItemExists(char prefix, char itemPrefixes[]) {
        for (int i = 0; i < size; i++) {
            if (itemPrefixes[i] == prefix) {
                return 1;
            }
        }
        return 0;
    }
    
    
    void displayMenu(char itemPrefixes[], int prices[]) {
        printf("\n***** SHOP MENU *****\n");
        printf("Fruit:\tPrices");
        for (int i = 0; i < size; i++) {
            printf("\n%c:\t%d", itemPrefixes[i], prices[i]);
        }
        printf("\n\n"); // Display menu
    }
    int purchaseItem(int budget, char itemPurchased, char itemPrefixes[], int prices[]) {
        printf("Your purchase has been successful!\n");
        printf("Purchase details\n");
        printf("---------------------------------\n");
        printf("Item selected : %c\n", itemPrefixes);
        printf("item price : %d\n", prices);
        printf("Remaining budget : %d\n", RemainingBudget);
        return 0;
    }
    
    
    int getPrices(char c, char itemPrefixes[], int prices[]) {
        for (int i = 0; i < size; i++) {
            if (c == itemPrefixes[i]) {
                return prices[i];  //get prices
            }
        }
        return -1;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,069
    Functions in C - Cprogramming.com

    You need to learn the difference between build and run-time errors.
    And, when you get an build error post it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread