Reposting my question froma few days ago since I had no response to it just in c
ID: 3671988 • Letter: R
Question
Reposting my question froma few days ago since I had no response to it just in case it was overlooked.
Enhance the C++ program such that a customer(use) can choose at most 3 different items. The enhanced program should be able to display the total cost with sub-totals This is to be done in the main without adding a new funtion. I have the implementation files and header files done I am not sure how I would go about answering this quesiton. Here is the code that I have and need to fix. Thank you for your time and help.
#include <iostream>
#include "cashRegister.h"
#include "dispenserType.h"
using namespace std;
void showSelection();
void sellProduct(dispenserType& product,
cashRegister& pCounter);
int main()
{
cashRegister counter;
dispenserType orange(100, 50);
dispenserType apple(100, 65);
dispenserType mango(75, 80);
dispenserType strawberryBanana(100, 85);
int choice; //variable to hold the selection
showSelection();
cin >> choice;
while (choice != 9)
{
switch (choice)
{
case 1:
sellProduct(orange, counter);
break;
case 2:
sellProduct(apple, counter);
break;
case 3:
sellProduct(mango, counter);
break;
case 4:
sellProduct(strawberryBanana, counter);
break;
default:
cout << "Invalid selection." << endl;
}//end switch
showSelection();
cin >> choice;
}//end while
return 0;
}//end main
void showSelection()
{
cout << "*** Welcome to Shelly's Juice Shop ***" << endl;
cout << "To select an item, enter " << endl;
cout << "1 for orange juice (50 cents)" << endl;
cout << "2 for apple juice (65 cents)" << endl;
cout << "3 for mango juice (80 cents)" << endl;
cout << "4 for strawberry banana juice (85 cents)" << endl;
cout << "9 to exit" << endl;
}//end showSelection
void sellProduct(dispenserType& product,
cashRegister& pCounter)
{
int amount; //variable to hold the amount entered
int amount2; //variable to hold the extra amount needed
if (product.getNoOfItems() > 0) //if the dispenser is not
//empty
{
cout << "Please deposit " << product.getCost()
<< " cents" << endl;
cin >> amount;
while (amount < product.getCost())
{
cout << "Please deposit another "
<< product.getCost()- amount
<< " cents" << endl;
cin >> amount2;
amount = amount + amount2;
}
if (amount >= product.getCost())
{
pCounter.acceptAmount(amount);
product.makeSale();
cout << "Collect your item at the bottom and "
<< "enjoy." << endl;
}
else
cout << "The amount is not enough. "
<< "Collect what you deposited." << endl;
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
<< endl << endl;
}
else
cout << "Sorry, this item is sold out." << endl;
}//end sellProduct
Explanation / Answer
You can add below mentioned code before calling switch case.
int totalchoice=0;
float totalPrice;
Inside switch you will be incrementing totalchoice using totalchoice++ and before calling showProduct you have to add. Also if can take one boolean varibale to know if item is sold out than you shouldn't increment total count.
if(totalchoice == 3){
break;
}
For total price you havr to add price every time in totalPrice varibale so in last you will have total price.
Let me know if you require more details because you have not provided the complete code I won't be able to provide running code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.