Thread: trying to print smallest total inputted number in a for loop

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    1

    Exclamation trying to print smallest total inputted number in a for loop

    Hello, all. I am relatively new to coding (and this forum!) and trying to code something where the user is going to several shops and buying ingredients for recipes. The user inputs the amount of shops they want to go to, the amount of ingredients they buy at each shop, and the price of each ingredient. Where I am struggling though is the end, I am trying to print the shop number with the lowest total cost and I just can't seem to figure it out. I'll post the code I have so far.

    Code:
     #include <stdio.h>
    
    
     int main() {
        //variable declaration
        int shops, i, j, ing, min_shop = 0;
        float price, min_cost = 0, total = 0;
    
    
        //ask about # of shops
        printf("Your mentor has tasked you with going to several different stores to place orders.\n");
        printf("How many shops will you be visiting?\n");
        scanf(" %d", &shops);
        min_shop = shops
    
    
        //ask about # of ingredients
        for(i = 1; i <= shops; i++){
            printf("You are at shop #%d.\n", i);
            printf("How many ingredients do you need at shop #%d?\n", i);
            scanf(" %d", &ing);
            //ask about price of ingredients
            for(j = 1; j <= ing; j++){
                printf("How much is ingredient %d?\n", j);
                scanf(" %f", &price);
                total = total + price;
                min_cost = total
            }
            //total cost at each shop
            printf("Your total at shop number %d is $%.2f.\n", i, total);
        }
    
    
        if (min_cost<total)
        printf("Your cheapest order was at shop #%d and cost $%.2f.\n", min_cost, );
    
    
       return 0;
    
    
     }
    This is what I have so far, obviously it is not perfect by any means lol but any help regarding calculating the cheapest order and printing which shop it was at would be immensely helpful. Thanks so much!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    It would look something like this
    Code:
           total = 0;  // for this shop
           for(j = 1; j <= ing; j++){
               printf("How much is ingredient %d?\n", j);
               scanf(" %f", &price);
               total = total + price;
           }
           if ( total < min_total ) {
           }
    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: 6
    Last Post: 11-11-2017, 01:26 AM
  2. function call to print the smallest number?
    By mrcisco_89 in forum C Programming
    Replies: 5
    Last Post: 03-16-2013, 03:30 AM
  3. Replies: 14
    Last Post: 10-07-2012, 06:43 PM
  4. How to invert an inputted number?
    By george7378 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2010, 01:03 PM
  5. How to determine and print smallest and largest values
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 09-28-2010, 07:05 AM

Tags for this Thread