# Thread: Need help, nested loops

1. ## 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
1 --> Beer
2 --> Diaper
0 --> Nothing

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

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. 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.

3. 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[] =
{
};
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;
}```