1
votes

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%.

1
I read the question about 10 times so far, and could not understand what you mean. Your problem seems to be trivial, but it is not phrased properly. Please edit it to explain what you want in correct English.akaAbdullahMateen
That code does not/could not produce that output. Please clarify what you DO get and what you WANT to get.Luke Borowy
I think your code is almost good. The issue is the line tips *= meal;. If you change tips 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 the tip_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
@akaAbdullahMateen sorry english is not my first language and I am new to programming. I made some changes and hope you understand what I meant. Sorry for the inconvenience.D A A U
@LukeBorowy I made some changes and hope you understandD A A U

1 Answers

3
votes

As I suggested in the comment, I think that the point breaking your loop is the fact that you change tips inside the loop itself and, in yuour example, in the first cycle you start with a tip of 0.05, then multiply it by 100, and at the next iteration you do not even enter because you do not comply with the condition tips < 0.26. I think it would be a good idea to keep separed the percentage from the tip itself. Somehting like this:

#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;

  // You do not need this, you can declare it in the loop.
  //double tip_frac = 0.0;

  //Prompt the user for number of tests
  cout << "Enter total meal price: ";
  cin >> meal;

  //Loop
  // Tax is fixed, can be calculated outisde the loop.
  tax = meal * taxrate;
  cout <<"Your tax is $ "<< tax << " and.." <<endl;

  for (double tip_frac = 0.05; tip_frac < 0.26; tip_frac +=0.05)
  {
    tips = meal * tip_frac;
    total = tips + tax + meal;
    // Just a small reshuffle of pieces to get the output you seek.
    // Always remember spaces to get a nice output.
    // Factor 100 is to pass from fraction to percentage.
    cout << "a " << tip_frac * 100 <<"% tip is $"<< tips << " giving a total of " << total <<endl;
  }

  //system("pause");
  // What do you want to do with this pause? 
  return 0;

} //end of main

you should always pay attention when you change the variable you are looping on, it's dangerous.

There are some other suggestions that might be given, like Why is "using namespace std;" considered bad practice?, but I think it is important for you to get the main part of this issue.