Need help, nested loops

This is a discussion on Need help, nested loops within the C Programming forums, part of the General Programming Boards category; I'm completely stuck. I've thought about this for probably about 4 hours now and can't think of a way to ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    15

    Need help, nested loops

    I'm completely stuck. I've thought about this for probably about 4 hours now and can't think of a way to do it. Could someone tell me what I need to do. Here's what I'm trying to accomplish:

    Write a program that requires user to enter ONE product choice at each visiting point. The choices are 1 for beer, 2 for diaper and 3 for bread. If another number is entered ( i.e. 0), that should mean no purchase was made at the current shop and the program should print out "Nothing purchased" on the screen and skip to the next location. Before the user enters a choice, the program should notify the user about which city and shop he is at. The output should be like this:

    You are at City # 1 and Shop # 1
    Please enter your product choice
    1 --> Beer
    2 --> Diaper
    3 --> Bread
    0 --> Nothing


    Please enter your product choice, enter 1 for beer, 2 for diaper, 3 for bread
    At the end the program should calculate the total tax, tax included total amount and the amount of each product purchased.
    Here is the price list (tax free):

    Beer: $10
    Diaper: $5
    Bread: $2

    Tax rate is %18.

    Code:
    #include <stdio.h>
    
    	main () {
    
    	int counter,city,shop,i;
    
    	counter=6;
    	shop=1;
    	city=1;
    
    	while (counter>0) {
    
    	printf("You are at City # %d and Shop # %d\nPlease enter your product choice\n1 --> Beer\n2 --> Diaper\n3 --> Bread\n0 --> Nothing\n",city,shop);
    	
    	shop=shop+1;
    	counter=counter-1;
    
    	if (shop==4) {
    	city=2;
    	shop=1;
    	
    	}
    
    	scanf("%d", &i);
    
    	}
    
    	}

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Where is the second inner loop?
    Why is the user input below the loop control statements?
    At least your loop will finish.
    Try naming variables to represent what they are doing.
    'i' is obviously the user choice, do something with it.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    I was bored.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define CITY_COUNT 2
    #define SHOP_COUNT_PER_CITY 3
    
    static const char * const products[] =
    {
       "Nothing", "Beer", "Diapers", "Bread"
    };
    static const int product_prices[] = { 0, 10, 5, 2 };
    static const double tax_rate = 0.18;
    
    static void display_shop_and_city(int shop, int city)
    {
       printf("You are at City # %d and Shop # %d\n", city, shop);
    }
    
    static void display_options()
    {
       int i = 1;
       for (; i < sizeof(*products) ; ++i)
       {
          printf("%d --> %s\n", i, products[i]);
       }
    
       printf("%d --> %s\n", 0, products[0]);
    }
    
    static int get_input(int shop, int city)
    {
       int selected_item = 0;
       do
       {
          display_shop_and_city(shop, city);
          display_options();
    
          scanf("%d", &selected_item);
       }
       while (selected_item < 0 || selected_item > sizeof(*products));
    
       if (0 == selected_item)
       {
          printf("Nothing purchased\n");
       }
    
       return selected_item;
    }
    
    static void print_purchased_items(int *purchased_items)
    {
       int i = 0;
       for (; i < sizeof(*products); ++i)
       {
          printf ("%s: %d\n", products[i], purchased_items[i]);
       }
    }
    
    static int get_subtotal(int *purchased_items)
    {
       int i = 0, subtotal = 0;
       for (i = 0 ; i < sizeof(*products) ; ++i)
       {
          subtotal += (purchased_items[i] * product_prices[i]);
       }
       return subtotal;
    }
    
    static double calculate_tax(int subtotal)
    {
       return subtotal * tax_rate;
    }
    
    int main()
    {
       int selected_item = 0;
       int shop = 0, city = 0;
    
       int *items_purchased = calloc (sizeof(*products),
                                      sizeof(*items_purchased));
    
       while (++city <= CITY_COUNT)
       {
          while (++shop <= SHOP_COUNT_PER_CITY)
          {
             selected_item = get_input(shop, city);
             ++items_purchased[selected_item];
          }
          shop = 0;
       }
    
       printf ("\n");
    
       print_purchased_items(items_purchased);
    
       printf("\n");
    
       int subtotal = get_subtotal(items_purchased);
    
       free (items_purchased);
    
       printf("Subtotal: $%.2f\n", (double)subtotal);
    
       double tax = calculate_tax(subtotal);
    
       printf("Tax: $%.2f\n", tax);
    
       printf("Total: $%.2f\n", subtotal + tax);
    
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 07:58 AM
  2. nested For loops
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 10:12 AM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 02:47 PM
  4. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  5. nested for loops and bank account interest help!!
    By webvigator2k in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 09:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21