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!
int main()
{
int i=1,a,pizza,toppings,price,small,medium,large;
char size;
printf("Welcome!\n");
printf("This is order %d.\n",i);
printf("How many pizzas would you like? ");
scanf("%d",&pizza);
while(pizza!=0)
{
while(cnumber(pizza)==1)
{
printf("Please enter a positive number, or 0 to quit.\n");
printf("How many pizzas would you like? ");
scanf("%d",&pizza);
}
for(a=1;a<=pizza;)
{
printf("What size pizza would you like (S, M, or L) for pizza 1 ? ");
scanf("%c",&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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
int main()
{
int i=1,a,pizza,toppings,price=0,total=0;
int size;
printf("Welcome!\n");
printf("This is order %d.\n",i);
printf("How many pizzas would you like? ");
scanf("%d",&pizza);
while(pizza!=0)
{
while(cnumber(pizza)==1)
{
printf("Please enter a positive number, or 0 to quit.\n");
printf("How many pizzas would you like? ");
scanf("%d",&pizza);
}
for(a=1;a<=pizza;a++)
{
printf("What size pizza would you like (S, M, or L) for pizza %d ? ",a);
scanf("%c",&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("You may choose from S, M, or L.\n");
printf("What size pizza would you like (S, M, or L) for pizza %d ? ",a);
scanf("%c",&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 ? "
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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? "
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
<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?!?!?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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';
}
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?
Last view of my program :
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';
}
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 ? "
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.
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;
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.
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? "
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)
<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?!?!?
I'm waiting for your helpful replies.Without your help I think I can't do all of my homework.
Who will help me?
It didn't work.If I use default same problem occurs again.
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?
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