C++

Malik A.

asked • 07/23/21

I did everything, it just the total and the subtotal!?

I did everything, it is just the total and the subtotal that get wrong.



here are the codes


#include <iostream>


#include <string>


#include <iomanip>




using namespace std;



void Go();

int Menu();

void Bill();

void Payment();



const double PRICE_GYRO = 7.00;

const double PRICE_MOUSSAKA = 8.50;

const double PRICE_SALAD = 3.75;

const double PRICE_DOLMA = 2.00;

const double PRICE_SODA = 2.80;

const double PRICE_COFFEE = 1.00;

const double PRICE_WATER = 2.00;


double subtotal = 0;

double total = 0;

const double TAX = 0.65;

const double TIP = 2;


int main()

{

Go();

Bill();

Payment();


}

int Menu()

{


int item;


cout << setprecision(2) << fixed;


cout << "\nEnter your choice from the menu below";

cout << "\n1) Gyro $" << PRICE_GYRO;

cout << "\n2) Moussaka $" << PRICE_MOUSSAKA;

cout << "\n3) Salad $" << PRICE_SALAD;

cout << "\n4) Dolma $" << PRICE_DOLMA;

cout << "\n5) Soda $" << PRICE_SODA;

cout << "\n6) Coffee $" << PRICE_COFFEE;

cout << "\n7) Water $" << PRICE_WATER;

cout << "\n8) End of Order " << endl;

cout << "Enter your menu choice>";

cin >> item;

if (item > 8)

{

cout << "\nInvalid choice...select again>";

cin >> item;

}



return item;


}

void Go()

{



double numGyro = 0;

double numMoussaka = 0;

double numSalad = 0;

double numDolma = 0;

double numSoda = 0;

double numCoffee = 0;

double numWater = 0;

int iMenuResult = 0;





while (iMenuResult != 8)

{


iMenuResult = Menu();



switch (iMenuResult)

{


case 1:

numGyro++;


break;


case 2:

numMoussaka++;


break;


case 3:

numSalad++;


break;


case 4:

numDolma++;


break;


case 5:

numSoda++;


break;


case 6:

numCoffee++;


break;


case 7:

numWater++;


break;


default:

break;


}








if (numGyro > 0)

{

subtotal = numGyro * PRICE_GYRO;


total += subtotal;



}




if (numMoussaka > 0)

{


subtotal = numMoussaka * PRICE_MOUSSAKA;


total += subtotal;



}




if (numSalad > 0)

{


subtotal = numSalad * PRICE_SALAD;


total += subtotal;



}




if (numDolma > 0)

{


subtotal = numDolma * PRICE_DOLMA;


total += subtotal;



}




if (numSoda > 0)

{


subtotal = numSoda * PRICE_SODA;


total += subtotal;


}




if (numCoffee > 0)

{


subtotal = numCoffee * PRICE_COFFEE;


total += subtotal;


}




if (numWater > 0)

{


subtotal = numWater * PRICE_WATER;


total += subtotal;


}



}




return;



}

void Bill()

{

cout << setprecision(2) << fixed;


cout << "\nFood Cost $" << static_cast<double>(subtotal);

cout << "\nTax Amount $" << TAX;

cout << "\nThe tip is $" << TIP;

cout << "\nTotal Bill $" << total + TAX + TIP;

}

void Payment()

{

cout << setprecision(2) << fixed;


double payment;


cout << "\nEnter your payment amount>";

cin >> payment;

while (payment < total + TAX + TIP)

{

cout << "\nError...amount paid id less than your bill of $" << total + TAX + TIP;

cout << "\nRe-enter your payment amount>";

cin >> payment;

}


cout << "\nYour total bill is $" << total + TAX + TIP << " and you paid $" << payment << ", so your change is $" << payment - (total + TAX + TIP);

}


in the codes, I can choose from numbers 1 -7 or 8 to get the result, but when I hit 8 the last choice that I chose add to the total! how could I let the program not add more price over the total when I hit 8. Also, the subtotal when it is 10 or more, it be int even though I let it be double??


it supposes to be like that in the end

https://mrkzgulfup.com/uploads/162705822537611.png

but I got this

https://mrkzgulfup.com/uploads/162705822545172.jpg

1 Expert Answer

By:

Kathryn C. answered • 07/23/21

Tutor
4.9 (74)

4+ years in tutoring and teaching with focus on memory management

Malik A.

thanks a lot for what you wrote, it helps me so much, and I solved the subtotal because of you, but the total is still wrong when I enter 8??
Report

07/24/21

Kathryn C.

Hi Malik, be sure to understand the loop in your program. I recommend. You run in a loop the totaling. Your program is like this: If I order a Gyro, the gyro count increases, it is greater than 0 so add it's price to the total. I order a water, *the gyro count is still greater than 0 so add the gyro price ONCE AGAIN to the total* water count is now one, and is greater than 0 thus, add to the total. Using output statements or cerr statements can really help you. I also recommend running through your code logic step by step with a pen and paper. You will see you add to your total every time you select an option from your menu, even if you already added it.
Report

07/24/21

Malik A.

thanks a lot, I looked again as you said and solved the "total"
Report

07/25/21

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.