Menu

Homework again!!!

#10lincoln
2007-12-18
2012-09-26
  • #10lincoln

    #10lincoln - 2007-12-18

    Question :

    Write a pizza-ordering program for your local Pizza Restaurant. Implement the program using separate functions for each major part of the program. Your final program should do the following:
    1.User may enter more than one order, one at a time.
    2.Each order may have more than one pizza. Zero pizzas quits.
    3.User selects a size (small, medium, or large) for each pizza.
    4.User selects number of toppings for each pizza
    NOTE: Store the total number of toppings for all pizzas in an order using a single variable.
    5.Program verifies order is correct. If not, user re-enters the order.
    6.Program produces an itemized receipt including tax and total.
    7.Use the following prices:
    1.a small pizza is $ 5
    2.a medium pizza is $ 7
    3.a large pizza is $ 9
    4.Toppings are $ 0.75 each
    5.Tax is 10 % of the subtotal.

    Here are examples of possible functions you may want to write (you may write more functions):
    1.A function that checks to make sure a valid number for the number of pizzas is entered, returns int.
    2.A function that checks to make sure valid letter for the pizza size is entered, returns char.
    3.A function that prints the total small, medium, and large pizzas and total toppings.
    4.A function that calculates cost of all items, subtotal, tax, and total and prints them.
    5.A function that calculates the tax (type float) and returns float.

    Here is an example output of the program. The user's input is shown in bold:

    Welcome!
    This is order 1.
    How many pizzas would you like? -3
    Please enter a positive number, or 0 to quit.
    How many pizzas would you like? 2
    What size pizza would you like (S, M, or L) for pizza 1 ? S
    How many toppings would you like on pizza 1 ? 4
    What size pizza would you like (S, M, or L) for pizza 2 ? X
    You may choose from S, M, or L.
    What size pizza would you like (S, M, or L) for pizza 2 ? L
    How many toppings would you like on pizza 2 ? 2
    Your order is:
    1 small pizza
    1 large pizza
    6 toppings

    Is this correct? Y
    Receipt:
    Pizza Order
    Order #1
    1 small pizza $ 5.00
    1 large pizza $ 9.00
    6 toppings $ 4.50

    subtotal       $ 18.50
    tax               $  1.85
    TOTAL       $ 20.35
    

    This is order 2
    How many pizzas would you like? 0
    Thanks!


    My source file :

    include<stdio.h>

    int cnumber(int pizza)
    {
    if(pizza<0)
    return 1;
    else
    return 0;
    }

    char cchar(char size)
    {
    if(size!='S' || size!='M' || size!='L')
    return 'a';
    else
    return 'b';
    }

    int main()
    {
        int i=1,a,pizza,toppings,price,small,medium,large;
        char size;
        printf(&quot;Welcome!\n&quot;);
        printf(&quot;This is order %d.\n&quot;,i);
        printf(&quot;How many pizzas would you like? &quot;);
        scanf(&quot;%d&quot;,&amp;pizza);
        while(pizza!=0)
        {
            while(cnumber(pizza)==1)
            {
                printf(&quot;Please enter a positive number, or 0 to quit.\n&quot;);
                printf(&quot;How many pizzas would you like? &quot;);
                scanf(&quot;%d&quot;,&amp;pizza);
            }
            for(a=1;a&lt;=pizza;)
            {
                printf(&quot;What size pizza would you like (S, M, or L) for pizza 1 ? &quot;);
                scanf(&quot;%c&quot;,&amp;size); 
            }
    
        }
        return 0;
    }
    

    It prints "What size pizza would you like (S, M, or L) for pizza 1 ? " two times.How can I solve this problem?What is wrong here?

     
    • #10lincoln

      #10lincoln - 2007-12-18

      Last view of my program :

      #include&lt;stdio.h&gt;
      

      int cnumber(int pizza)
      {
      if(pizza<0)
      return 1;
      else
      return 0;
      }

      char cchar(char size)
      {
      if(size!='S' || size!='M' || size!='L')
      return 'b';
      else
      return 'c';
      }

      int main()
      {
          int i=1,a,pizza,toppings,price=0,total=0;
          int size;
          printf(&quot;Welcome!\n&quot;);
          printf(&quot;This is order %d.\n&quot;,i);
          printf(&quot;How many pizzas would you like? &quot;);
          scanf(&quot;%d&quot;,&amp;pizza);
          while(pizza!=0)
          {
              while(cnumber(pizza)==1)
              {
                  printf(&quot;Please enter a positive number, or 0 to quit.\n&quot;);
                  printf(&quot;How many pizzas would you like? &quot;);
                  scanf(&quot;%d&quot;,&amp;pizza);
              }
              for(a=1;a&lt;=pizza;a++)
              {
                  printf(&quot;What size pizza would you like (S, M, or L) for pizza %d ? &quot;,a);
                  scanf(&quot;%c&quot;,&amp;size);
                  switch(size)
                  {
                  case 'S' :
                  price=5;
                  break;
      
                  case 'M' :
                  price=7;
                  break;
      
                  case 'L' :
                  price=9;
                  break;
      
                  case '\n' :
                  case '\t' :
                  case ' ' :
                  break;
                  }
      
                      while(cchar(size)=='b')
                  {
                      printf(&quot;You may choose from S, M, or L.\n&quot;);
                      printf(&quot;What size pizza would you like (S, M, or L) for pizza %d ? &quot;,a);
                      scanf(&quot;%c&quot;,&amp;size);
                      switch(size)
                  {
                  case 'S' :
                  price=5;
                  break;
      
                  case 'M' :
                  price=7;
                  break;
      
                  case 'L' :
                  price=9;
                  break;
      
                  case '\n' :
                  case '\t' :
                  case ' ' :
                  break;
                  }
                  }
      
              }
      
          }
          return 0;
      }
      

      It's still some mistake.It prints "What size pizza would you like (S, M, or L) for pizza 1 ? " , "You may choose from S, M, or L." , "What size pizza would you like (S, M, or L) for pizza 1 ? " in order.Then i can enter a letter.But it must print only "What size pizza would you like (S, M, or L) for pizza 1 ? " and i should be able to enter a letter if I don't enter a letter like S,M,L then it should warn me "You may choose from S, M, or L." and "What size pizza would you like (S, M, or L) for pizza 1 ? "

       
      • Osito

        Osito - 2007-12-19

        I think Clifford answered this same question a couple days ago. It has to do with the way scanf removes characters from the buffer. When you do this:

        scanf("%d",&pizza);

        It takes out the number you typed but leaves the newline. When it gets to the next scanf the newline is still in the buffer and it doesn't wait for more input. It takes the newline as the next thing to scan. That's why it skips right through the first time around.

        Some other notes:

        The variable 'size' should be character not int, since you're scanning in a character.

        This is unusual: for(a=1;a<=pizza;a++) and is normally written as this: for(a=0;a<pizza;a++). I would suggest getting in the habit of doing it the conventional way or you're going to have trouble reading other people's code in the future.

        Where you have this in your switch:

        case '\n' :
        case '\t' :
        case ' ' :
        break;

        You should instead use:

        default:
        break;

        That will handle all other cases, so you don't have to explicitly handle the whitespace characters.

         
    • BiT

      BiT - 2007-12-18

      lots of issues with the code above but you need to look at your loops

      How many times are you looping through stuff in each WHILE loop? or in the FOR loop what are you doing after you meet a condition? ending the program, going back to the beginning or jumping to some other function?

      Good Ideas, always assign values to variables if in doubt 0 works great. int i=0; is better then int i;

      Try to get in the habit of declaring your variables on seperate lines.

      int i,o,u; works but is bad practice

      what you should do is
      int i=0;
      int o=0;
      int u=0;

       
    • #10lincoln

      #10lincoln - 2007-12-18

      I just started to do this program.Only think I wonder is how to fix printing "What size pizza would you like (S, M, or L) for pizza 1 ? " two times.I know I declared unused variables.But when I get rid of this problem I think it will be more easy for me to do rest of the work.

       
    • BiT

      BiT - 2007-12-18

      I will not tell you the answer(since it is homework) but I did point you in the right direction.

      "How many times are you looping through stuff in each WHILE loop? or in the FOR loop? What are you doing after you meet a condition? ending the program, going back to the beginning or jumping to some other function? "

       
    • #10lincoln

      #10lincoln - 2007-12-18

      I know I must increment variable "a" by 1.And i can finish the loop when i enter 0 for variable "pizza" as i showed it below.

      while(pizza!=0)

       
    • BiT

      BiT - 2007-12-18

      <i can finish the loop when i enter 0 for variable "pizza">

      so when does that happen? You are not listening to what I am telling you. Go look at your loops, when do you exit out of them or are you just running the loop over and over again. When does the value change? if it never changes how do you exit your loop?!?!?

       
    • #10lincoln

      #10lincoln - 2007-12-19

      I'm waiting for your helpful replies.Without your help I think I can't do all of my homework.

       
    • #10lincoln

      #10lincoln - 2007-12-19

      Who will help me?

       
    • #10lincoln

      #10lincoln - 2007-12-19

      It didn't work.If I use default same problem occurs again.

       
      • Osito

        Osito - 2007-12-19

        Did you even read my post, or just skip to the end and try the last thing I mentioned? I pointed out four potential problems, the last three of which were unrelated to your bug and simply bad coding practice in my opinion. The first thing I mentioned addressed your bug. Did you check out Clifford's recent post on the subject? Did you read a manual explaining how scanf works?

         
      • Wayne Keen

        Wayne Keen - 2007-12-20

        It is important when you are getting help with a problem, and getting a number
        of hints and suggestions - that you

        (1) Take care to make sure you read through all of them

        and

        (2) Show the exact changes you made to your code in response to
        the suggestions - this shows (a) What you did and (b) How you implimented
        it.

        To do (2) you need to post (and repost) your Basic 3 at every step.
        Avoid statements of the form "I tried what you said and it didn't
        work"

        The perverse nature of the nature of this process is that the bigger
        your hurry, the way to get things done faster is to slow down and be
        methodical.

        Finally, conveying a strong sense of urgency in your posts frequently
        does NOT make people want to help you more. In fact it frequently
        has the opposite effect.

        Also, try to avoid the implication that you are not interested in working
        through your problems, and just want someone to fix your code so you
        can turn it in.

        Wayne

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.