In: Computer Science
I'm having a bit of an issue with one of my C++ labs. It's not for score but I need to understand it so I can move on.
A restaurant servers burgers for $8 and salads for $7.
When the customer selects a burger they must choose if they would like cheese. If they do, cheddar costs an additional 25 cents while pepper jack is 50 cents.
When the customer selects a salad they must choose if they would like no dressing, ranch for 10 cents more, and honey mustard for 15 cents more.
Customers can buy a drink for $1.50
Use the question prompts already declared as strings to ask the user for responses and then calculate the total cost the customer owes.
The code I wrote is as follows:
#include <iostream>
using namespace std;
int main() {
int burgerOrSalad, cheese, kindOfCheese, dressing,
drink;
string q1 = "Would you like a 1) burger or 2) a
salad?";
string q2 = "Would you like cheese? 0) No or 1)
Yes";
string q3 = "What kind of cheese 1) Cheddar or 2)
Pepper jack?";
string q4 = "What kind of dressing would you like 0)
None, 1) Ranch, 2) Honey Mustard?";
string q5 = "Would you like a drink? 0) No, 1)
Yes";
cout << q1 << endl;
cin >> burgerOrSalad;
double cost = 0;
cout << q2 << endl;
cin >> cheese;
cout << q3 << endl;
cin >> kindOfCheese;
cout << q4 << endl;
cin >> dressing;
cout << q5 << endl;
cin >> drink;
if ( burgerOrSalad == 1 ) {
cost = 8.00;
}
if ( cheese == 1 ) {
}
else if (
kindOfCheese == 1 ) {
cost = 8.25;
}
else if (
kindOfCheese == 2 ) {
cost = 8.50;
}
if ( burgerOrSalad == 2 ) {
cost = 7.00;
}
else if ( dressing == 1 ) {
cost = 7.10;
}
else if ( dressing == 2 ) {
cost = 7.15;
}
if ( drink == 1 ) {
cost = 1.50;
}
cout << "Total cost of selected meal is "
<< cost << endl;
return 0;
}
How can I add the cost if the input is 1 1 1 1?
I'm so lost.
#include <iostream> using namespace std; int main() { int burgerOrSalad, cheese, kindOfCheese, dressing, drink; string q1 = "Would you like a 1) burger or 2) a salad?"; string q2 = "Would you like cheese? 0) No or 1) Yes"; string q3 = "What kind of cheese 1) Cheddar or 2) Pepper jack?"; string q4 = "What kind of dressing would you like 0) None, 1) Ranch, 2) Honey Mustard?"; string q5 = "Would you like a drink? 0) No, 1) Yes"; cout << q1 << endl; cin >> burgerOrSalad; double cost = 0; // if customer wants burger then only ask him for cheese if(burgerOrSalad == 1) { cout << q2 << endl; cin >> cheese; // ask q3 only if person wants cheese if(cheese == 1) { cout << q3 << endl; cin >> kindOfCheese; } } else { cout << q4 << endl; cin >> dressing; } cout << q5 << endl; cin >> drink; if ( burgerOrSalad == 1 ) { cost = 8.00; if ( cheese == 1 ) { if ( kindOfCheese == 1 ) { cost = 8.25; } else if ( kindOfCheese == 2 ) { cost = 8.50; } } } else if ( burgerOrSalad == 2 ) { cost = 7.00; // only in case of salad, check the dressing price: if ( dressing == 1 ) { cost = 7.10; } else if ( dressing == 2 ) { cost = 7.15; } } // if customer wants drink also, apart from burger or salad, // then add the drink's cost to total cost whatsoever till now. if ( drink == 1 ) { cost += 1.50; } cout << "Total cost of selected meal is " << cost << endl; return 0; }
************************************************** I have added comments which will help you. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.