The top half is what I need to do and the second is what I did. I am doing somet
ID: 3888225 • Letter: T
Question
The top half is what I need to do and the second is what I did. I am doing something wrong but I don't understand what I am doing wrong.
Explanation / Answer
Some mistakes in the code :
1. When you check if the item number is correct , the statements should be enclosed in { } . Because there are no { }, the compiler assumes only the 1st line following if( ) is to be executed for true condition and the following return 0; will always be executed. So when you enter an invalid choice, you get the error message and the program terminates since return 0 will be executed. When you enter a valid choice, there is no error message but the return 0 terminates the program.
Suggestion: Always its better to use { } even when there is single statement. This way we are clear which statements to be executed for if-part and which ones for else-part
2. When order < 0, there are no statements to execute. The compiler will complain because else is appearing iwthout an if block of statements.
3. Also when you calculate the cost, you are not considering the order quantity. So the calculated value would be only for 1 item
==============
Given below is the code that funcitons as desired. I have a suggestion for you, when you paste your code in question, always copy paste as text rather than image. This way it is easier to modify your code rather than typing it out again. Especially when the code runs into pages, it will be easier to copy paste text. Also the existing comments and formatting will not be lost.
Hope the answer helped. If it did, please do rate the answer. Thank you very much.
#include <iostream>
using namespace std;
int main()
{
float cost;
const float rate = 1.05;
int orders = 0;
int choice = 0;
cout << "1. McDonalds" << endl;
cout << "2. Wendy's" << endl;
cout << "3. Chick-fil-A" << endl;
cout<< "Which restaurant would you like to order from [1-3]? ";
cin >> choice;
if(choice < 1 || choice > 3)
{
cout << "Invalid restaurant choice" << endl;
}
else
{
if(choice == 1)
{
//print menu for McDonald
cout << "Welcome to McDonald's" << endl;
cout << "1. Big Mac $5.99" << endl;
cout << "2. Egg McMuffin $2.34" << endl;
cout << "3. Signature Sriracha $5.46" << endl;
cout << "4. Pico Guacamole $6.29" << endl;
cout << "5. Signature Sriracha Buttermilk Cripsy Chicken Sandwich $ 6.45" << endl;
cout << "What would you like today [1-5]? " ;
cin >> choice;
if(choice < 1 || choice > 5)
{
cout << "Invalid item chosen" << endl;
}
else
{
cout << "How many orders? ";
cin >> orders;
if(orders <= 0)
{
cout << "Invalid order quantity." << endl;
}
else
{
if(choice == 1) cost = orders * 5.99 * rate;
else if(choice == 2) cost = orders * 2.34 * rate;
else if(choice == 3) cost = orders * 5.46 * rate;
else if(choice == 4) cost = orders * 6.29 * rate;
else if(choice == 5) cost = orders * 6.45 * rate;
cout << "Your order costs $" << cost << endl;
}
}
}
else if(choice == 2)
{
//print menu for Wendy
cout << "Welcome to Wendy's" << endl;
cout << "1. Dave's Single $6.99" << endl;
cout << "2. Son of Baconatar $4.36$" << endl;
cout << "3. Jr Hamburger $4.44$" << endl;
cout << "4. Spicey Chicken Sandwich $7.50" << endl;
cout << "5. Spicey Asian Club $7.50" << endl;
cout << "What would you like today [1-5]? " ;
cin >> choice;
if(choice < 1 || choice > 5)
{
cout << "Invalid item chosen" << endl;
}
else
{
cout << "How many orders? ";
cin >> orders;
if(orders <= 0)
{
cout << "Invalid order quantity." << endl;
}
else
{
if(choice == 1) cost = orders * 6.99 * rate;
else if(choice == 2) cost = orders * 4.36 * rate;
else if(choice == 3) cost = orders * 4.44 * rate;
else if(choice == 4) cost = orders * 7.5 * rate;
else if(choice == 5) cost = orders * 7.5 * rate;
cout << "Your order costs $" << cost << endl;
}
}
}
else
{
//print menu for Chick fil A
cout << "Welcome to Chick-Fil-A" << endl;
cout << "1. Chicken Sandwich $3.99" << endl;
cout << "2. Delux Chicken Sandwich $4.99" << endl;
cout << "3. Spicy Sandwich $3.99" << endl;
cout << "4. Spicy Deluxe Sandwich $4.99" << endl;
cout << "5. Nuggets $5.99" << endl;
cout << "What would you like today[1-5]? " ;
cin >> choice;
if(choice < 1 || choice > 5)
{
cout << "Invalid item chosen" << endl;
}
else
{
cout << "How many orders? ";
cin >> orders;
if(orders <= 0)
{
cout << "Invalid order quantity." << endl;
}
else
{
if(choice == 1) cost = orders * 3.99 * rate;
else if(choice == 2) cost = orders * 4.99 * rate;
else if(choice == 3) cost = orders * 3.99 * rate;
else if(choice == 4) cost = orders * 4.99 * rate;
else if(choice == 5) cost = orders * 5.99 * rate;
cout << "Your order costs $" << cost << endl;
}
}
}
}
}
output
$ g++ restaurants.cpp
$ ./a.out
1. McDonalds
2. Wendy's
3. Chick-fil-A
Which restaurant would you like to order from [1-3]?
1
Welcome to McDonald's
1. Big Mac $5.99
2. Egg McMuffin $2.34
3. Signature Sriracha $5.46
4. Pico Guacamole $6.29
5. Signature Sriracha Buttermilk Cripsy Chicken Sandwich $ 6.45
What would you like today [1-5]? 3
How many orders? 4
Your order costs $22.932
$ ./a.out
1. McDonalds
2. Wendy's
3. Chick-fil-A
Which restaurant would you like to order from [1-3]? 5
Invalid restaurant choice
$ ./a.out
1. McDonalds
2. Wendy's
3. Chick-fil-A
Which restaurant would you like to order from [1-3]? 0
Invalid restaurant choice
$ ./a.out
1. McDonalds
2. Wendy's
3. Chick-fil-A
Which restaurant would you like to order from [1-3]? 2
Welcome to Wendy's
1. Dave's Single $6.99
2. Son of Baconatar $4.36$
3. Jr Hamburger $4.44$
4. Spicey Chicken Sandwich $7.50
5. Spicey Asian Club $7.50
What would you like today [1-5]? 7
Invalid item chosen
$ ./a.out
1. McDonalds
2. Wendy's
3. Chick-fil-A
Which restaurant would you like to order from [1-3]?
3
Welcome to Chick-Fil-A
1. Chicken Sandwich $3.99
2. Delux Chicken Sandwich $4.99
3. Spicy Sandwich $3.99
4. Spicy Deluxe Sandwich $4.99
5. Nuggets $5.99
What would you like today[1-5]? 3
How many orders? -1
Invalid order quantity.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.