Search code examples
arrayscpointersiteratorsum

C - accessing variable in function to print total sum when user exits program


So I'm creating an online grocery store where each product is assigned a name, product id, and price. Every time the user purchases a product through entering the associated id, their total cost to pay gets updated.

Example of products & output:

Products:
Name: orange, id: 100, price: 4.50
Name: banana, id: 101, price: 2.00
Name: milk, id: 102, price: 3.00

User presses '3':
User enters: 101
Current price to pay: 2.00

User presses '3':
User enters: 102:
Current price to pay: 5.00

User presses '3':
User enters: 101
Current price to pay: 7.00

User presses '0':
Total price to pay is: $7.00

So calculating and printing the current price to the user works, but I'm having trouble accessing the variable 'sum' that represents the total price the user has to pay when the user decides to exit by entering '0'. I thought about maybe returning the sum variable somehow or even creating a new function and calling it but I'm not sure how to go about it and would appreciate some help. As you can see in main.c, when the user enters '0' I added a print statement 'printf("Total price to pay: $%f \n", sum); ' but the problem is sum doesn't exist here so I have no way of accessing the total cost.

As a side note, I tried to declare and initialize the variable sum inside purchaseProd() but it wouldn't actually calculate the sum of all products, and would instead only output the current price of the product they want to specifically buy. So for example, in my output above, the current price to pay after purchasing product id '102' would be $3.00 because that's the price of the product, even though I would like it to print out $5.00. This was fixed by declaring the sum variable outside of the function, making it a global variable.

I'm trying to avoid declaring global variables as I was told this is bad practice, and was wondering if there is a way to get my current price to calculate correctly other than making sum a global variable? Is there a way to maybe instead declare it in main() and then get access to it when I actually need to add the prices of different purchases to it?

main.c

int main(){
    int choice;
    while(1){
        printf("(1) Print inventory \n");
        printf("(2) Add product stock \n");
        printf("(3) Buy product \n");
        printf("(0) Exit \n");

        printf("Enter choice: ");
        scanf("%d", &choice);
        
        if(choice == 0){
            printf("Total price to pay: $%f \n", sum); //how to access total sum variable?
            exit(0);
        }
        if(choice == 1){
        }
        if(choice == 2){
        }
        if(choice == 3){
            int enterId;
            printf("Product id: ");
            scanf("%d", &enterId);  
            purchaseProd(p, enterId);
        }
    }
    return 0;
}

inventory.c

float sum = 0;
int purchaseProd(ProdCollectionType* prodCollection, int getId){
    //int sum = 0;
    for(i = 0; i < prodCollection->numProducts; ++i){
        if(prodCollection->products[i]->id == getId){
            sum += prodCollection->products[i]->price;
            printf("Current price to pay: $%.2f \n", sum);
          }
        }
    }
}

Solution

  • You want this:

    int main(){
        int choice;
        float sum = 0;    // add this
    
        while(1){
            ...
            if(choice == 3){
                int enterId;
                printf("Product id: ");
                scanf("%d", &enterId);  
                sum = sum + purchaseProd(p, enterId);   // change here
            }
        }
        return 0;
    }
    

    // float sum = 0;                                      // remove this
    float purchaseProd(ProdCollectionType* prodCollection, int getId){
        float sum = 0;                                      // add this
        for(i = 0; i < prodCollection->numProducts; ++i){
            if(prodCollection->products[i]->id == getId){
                sum += prodCollection->products[i]->price;
                printf("Current price to pay: $%.2f \n", sum);
              }
            }
        }
    
       return sum;                                         // add this
    }