Thread: using loops to compare pizza prices

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

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

    please help! thank you.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    may this help you?

    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. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    so i came up with this...

    Code:
    #include <stdio.h>
    
            /*organize a structure*/
            struct PizzaDeals
                    {
                            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*/
            struct PizzaDeals pizzaDeals[num_deals];
    
            /*read in the names of pizzerias*/
            /*read in the prices*/
            /*read in the diameters*/
            /*store the information in arrays*/
            count = 0;
            for (i=1; i <= num_deals; i++)
                    {
                    printf("enter pizza name: ");
                    scanf("%s", &pizzaDeals[i].PizzaCompanyName);
                    prinft("enter size: ");
                    scanf("%f", &pizzaDeals[i].pizzaSize);
                    prinft("enter price: ");
                    scanf("%f", &pizzaDeals[i].price);
                    count++;
                    }
    
            /*decipher which pizza is the cheapest*/
            /*use for loop*/
            for( i = 0; i <count; i++)
                    {
                    if( pizzaDeals[i].price/pizzaDeals[i].pizzaSize < bestPrice)
                            {
                            bestPrice = pizzaDeals[i].price/pizzaDeals[i].pizzaSize;
                            bestIndex = i;
                            }
                    }
            
            /*print out the information*/
            printf("Your best deal tonight is %s",pizzaDeals[i].PizzaCompanyName);
            
            
    
            
    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.
    any advise is apperciated.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    the error message from my compiler


    Code:
    #include <stdio.h>
      10.
      11.         /*organize a structure*/
      12.         struct PizzaDeals
      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*/
      36.   struct PizzaDeals pizzaDeals[num_deals];
    >>> parse error before `struct'
      37.
      38.   /*read in the names of pizzerias*/
      39.   /*read in the prices*/
      40.   /*read in the diameters*/
      41.   /*store the information in arrays*/
      42.   count = 0;
      43.   for (i=1; i <= num_deals; i++)
      44.           {
      45.           printf("enter pizza name: ");
      46.           scanf("%s", &pizzaDeals[i].PizzaCompanyName);
    >>> `pizzaDeals' undeclared (first use in this function)
    >>> (Each undeclared identifier is reported only once
    >>> for each function it appears in.)
      47.           prinft("enter size: ");
      48.           scanf("%f", &pizzaDeals[i].pizzaSize);
      49.           prinft("enter price: ");
      50.           scanf("%f", &pizzaDeals[i].price);
      51.           count++;
      52.           }
      53.
      54.         /*decipher which pizza is the cheapest*/
      55.         /*use for loop*/
      56.         for( i = 0; i <count; i++)
      57.                 {
      58.                 if( pizzaDeals[i].price/pizzaDeals[i].pizzaSize < bestPrice)
      59.                         {
      60.                         bestPrice = pizzaDeals[i].price/pizzaDeals[i].pizzaSize;
      61.                         bestIndex = i;
      62.                         }
      63.                 }  
      64.
      65.   /*print out the information*/
      66.   printf("Your best deal tonight is %s",pizzaDeals[i].PizzaCompanyName);
      67.
      68.
      69.
      70.
      71. return 0;   
      72.
      73. }
    what is wrong with it?
    will it even work???

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

    At the end, do
    free( pizzaDeals );

    > for (i=1; i <= num_deals; i++)
    Arrays, whether statically or dynamically allocated start at subscript 0
    So
    for (i=0; i < num_deals; i++)
    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. Algebra question
    By Dino in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 01-18-2008, 01:39 PM
  2. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 12:51 PM
  3. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  4. Design My Pizza.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 10-19-2002, 02:32 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM