using loops to compare pizza prices

This is a discussion on using loops to compare pizza prices within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that prompts the user to enter different pizza size combinations for easy comparison. ...

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

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.

36. struct PizzaDeals pizzaDeals[num_deals];
>>> 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
1. struct PizzaDeals *pizzaDeals;
at the start of the function, along with all the other variables.

2. pizzaDeals = malloc ( num_deals * sizeof *pizzaDeals );
where you have your variable array at the moment. This will allocate the space for you to use.
Don't forget to include stdlib.h at the start of the module.