I am making a program to calculate taxes and tips based on the meal price. Is there anyway for me to loop the cout
and print different percentages for each cout
for 5 times?
This is the code I wrote but I am stuck, I can write 5 cout
but I need to use looping so I think there must be a way to print with cout
until it stops.
#include <iostream>
using namespace std;
int main( )
{
//Declare variables
int counter = 0;
const double taxrate = 0.0625;
double meal = 0.00;
double tax = 0.00;
double tips = 0.00;
double total = 0.00;
double tip_percentage = 0.00;
//Prompt the user for number of tests
cout << "Enter total meal price: ";
cin >> meal;
//Loop
for (double tips_percentage = 0.05; tips_percentage < 0.26; tips_percentage +=0.05)
{
tax = meal * taxrate;
tips = tip_percentage * meal;
total = tips + tax + meal;
cout <<"Your tax is $"<< tax << "and.." <<endl;
cout << "a" << tip_percentage * 100<<"% tip is"<< tips << "giving a total of" << total <<endl;
}
system("pause");
return 0;
} //end of main
taxrate is constant 6.25% and the output is something like this.
a 5% tip is $... your total is $...
a 10% tip is $... your total is $...
a 15% tip is $... your total is $...
a 20% tip is $... your total is $...
a 25% tip is $... your total is $...
instead writing 5 cout, can I do a loop and print it 5 times?
For example, if the meal is $100, it will calculate the tax price which is 6.25% and then show the tips %. After that it will add everything together. So, 6.25% of $100 is $6.25 and 5% of the tip is $5. The total would be $111.25. The taxrate is constant so it will always be 6.25%.
tips *= meal;
. If you changetips
value inside the loop, it will go out of the loop quite fast because in few interactions you will get tips >= 0.26. I can suggest to you to keep variables separed: one is thetip_percentage
(inserted by the user), another is the actual tip. It is a good habit to not change the loop variable unless you know what you are doing. – chiappar