# Thread: using loops to compare pizza prices

1. ## using loops to compare pizza prices

I am trying to write a program that prompts the user to enter different pizza size combinations for easy comparison. The program will decipher which pizzeria offers the cheapest pizza per square inch. The problem is, we do not know how many offers there are each night. one night, we could have 4 deals...next night we could have 5 deals...I was thinking of using loops to let the user decide how many deals there are to enter. how do I do that?

so I got this. which will work but I do not know how to go on from there..

insert
Code:
```int i, num_deals;
/* do something here to read a number of deals */
printf("Enter th4e number of deals\n");
scanf("%d",num_deals);

for (i=1; i <= num_deals; i++) {
/* handle deal number i here */
}```

should I use an array? if so, how do you get started on that? I have no idea how to link the information the loop statement reads into an array...and have the array tell me which one of the answers is the lowest number so it can spit it out to the user and tell them which one is cheap...

i probably will have to use strings so the user can tell which pizzaria specifically the deals are the cheapest rather than base them on numbers.....

Code:
```#include <stdio.h>

int main(){

int i, num_deals;
printf("Enter th4e number of deals\n");
scanf("&#37;d",&num_deals);

int deals[num_deals];

for (i=0; i < num_deals; i++) {
printf("Enter deal #%d: ",i+1); scanf("%d",&deals[i]);
}
//Example of using the array elements
for (i=0; i < num_deals; i++) {
printf("Deal #%d: %d \n",i+1,deals[i]);
}

}```
be aware of scanf: You have to pass an address for num_deal!

3. Code:
`int deals[num_deals];`
declaring arrays with variables for size is not standard and doesn't work in all compilers. Even declaring variables after the first code statement is breaking the rules for C89, which is the standard you can KNOW is available today - most compilers support C99 style "variables anywhere", but arrays SHOULD have a constant variable even there.

--
Mats

4. so i came up with this...

Code:
```#include <stdio.h>

/*organize a structure*/
{
char PizzaCompanyName[32];
float pizzaSize;
float price;
};
int main ()
{

int num_deals;          /*input- how many deals there are tonight*/
int count;              /*internal loop variable*/
int i;                  /*internal loop variable*/
float bestIndex = 0;
float bestPrice = 99999.99;

/*Prompt the user*/
printf ("Welcome to pizza comparator \n");

/*read how many offers there are that night*/
printf ("Enter the number of deals currently available \n");
scanf ("%d", &num_deals);

/*use for loops*/

/*read in the names of pizzerias*/
/*store the information in arrays*/
count = 0;
for (i=1; i <= num_deals; i++)
{
printf("enter pizza name: ");
prinft("enter size: ");
prinft("enter price: ");
count++;
}

/*decipher which pizza is the cheapest*/
/*use for loop*/
for( i = 0; i <count; i++)
{
{
bestIndex = i;
}
}

/*print out the information*/

return 0;

}```

it does not compile. the compiler says pizzaDeals is undefined and gives me parse error.
I dont even know if it will work.

5. the error message from my compiler

Code:
```#include <stdio.h>
10.
11.         /*organize a structure*/
13.                 {
14.                 char PizzaCompanyName[32];
15.                 float pizzaSize;
16.                 float price;
17.                 };
18. int main ()
19. {
20.
21.   int num_deals;          /*input- how many deals there are tonight*/
22.   int count;              /*internal loop variable*/
23.   int i;                  /*internal loop variable*/
24.   float bestIndex = 0;
25.         float bestPrice = 99999.99;
26.
27.   /*Prompt the user*/
28.   printf ("Welcome to pizza comparator \n");
29.
30.   /*read how many offers there are that night*/
31.   printf ("Enter the number of deals currently available \n");
32.   scanf ("%d", &num_deals);
33.
34.
35.   /*use for loops*/
>>> parse error before `struct'
37.
38.   /*read in the names of pizzerias*/
41.   /*store the information in arrays*/
42.   count = 0;
43.   for (i=1; i <= num_deals; i++)
44.           {
45.           printf("enter pizza name: ");
>>> `pizzaDeals' undeclared (first use in this function)
>>> (Each undeclared identifier is reported only once
>>> for each function it appears in.)
47.           prinft("enter size: ");
49.           prinft("enter price: ");
51.           count++;
52.           }
53.
54.         /*decipher which pizza is the cheapest*/
55.         /*use for loop*/
56.         for( i = 0; i <count; i++)
57.                 {
59.                         {
61.                         bestIndex = i;
62.                         }
63.                 }
64.
65.   /*print out the information*/
67.
68.
69.
70.
71. return 0;
72.
73. }```
what is wrong with it?
will it even work???

>>> parse error before `struct'
It's only C99 (the new C standard), or various compiler specific extensions which permit either declarations in the middle of code, or arrays with a variable length.

Two things
at the start of the function, along with all the other variables.