I'm having trouble with getting the correct tip

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dan5348
    New Member
    • Oct 2014
    • 5

    I'm having trouble with getting the correct tip

    I'm having trouble with getting the correct tip:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    class Tips {
      private:
      double taxRate;
      
      public:
      Tips();
      Tips(double tRate);
    
      double computeTip(double totalBill, double tipRate);
    			
    };
    
    Tips::Tips()
    {
      taxRate = 0.065;
    }
    Tips::Tips(double tRate)
    {
      taxRate = tRate;
    }
    double Tips::computeTip(double totalBill, double tipRate)
    {
      return (totalBill + (totalBill * taxRate));
    }
    
    int main()
    {
      int    choice;
      double charge,
      bonus,
      saleTx;
    	
      bool running = true;
    
      while (running)
      {
    
        cout << "Please enter the amount before tax.";
        cin >> charge;
    	
        if (charge < 0)
        { cout << "Invalid entry. Negative numbers are not accepted.";
          cout << "Please enter only positive values";
          cin >> charge;
        }
    	
        cout << "Please enter the sales tax charged to the bill";
        cin >> saleTx;
    	
        Tips tips(saleTx);
    	
        cout << "Please enter the tip rate given";
        cin >> bonus;
    	
        if (bonus < 0)
        { cout << "Invalid entry. Negative numbers are not accepted.";
          cout << "Please enter only postive values";
          cin >> bonus;
        }
    		
        cout << "Your tip is: " << tips.computeTip(charge, bonus) << endl;
        return 0;
      }
    }
    Last edited by Frinavale; Nov 12 '14, 05:00 PM. Reason: Added the question posted in the title of the thread to the body of the thread so that the code has context and added code tags and corrected code indentation to make the code more readable.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Method computeTip() makes no use of the tipRate argument.

    Comment

    • dan5348
      New Member
      • Oct 2014
      • 5

      #3
      what are you saying?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        computeTip() has two parameters: totalBill and tipRate. However, the body of the function makes no use of the tipRate argument. Thus, when you call computeTip(char ge,bonus) near the bottom of your code, the bonus argument is not used.

        Comment

        • dan5348
          New Member
          • Oct 2014
          • 5

          #5
          Thanks for the help

          ok I added the tiprate
          Code:
          int main()
          {
            int    choice;
            double charge,
            tiprate,
            bonus,
            saleTx;
          	
            bool running = true;
          but the math isn't working still. How do I fix that?
          Last edited by Frinavale; Nov 12 '14, 05:00 PM. Reason: Added code tags and formatted the code so that it is more readable.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Dan5348,

            Your math isn't working because, like donbock stated, your computeTip method needs to be modified to properly do the math.

            Currently the method does this:
            Code:
             double Tips::computeTip(double totalBill, double tipRate)
            {
              return (totalBill + (totalBill * taxRate));
            }
            Now in your main method you are retrieve information for the charge,saleTx, and bonus.

            But your computeTip method only expects the total bill and tip rate to be provided to it.

            Based on what you are retrieving from your user, you should be calling the computeTip method like this:
            Code:
             tips.computeTip(charge, saleTx);
            But you are calling the method like this:
            Code:
             tips.computeTip(charge, bonus)
            Now, what doesn't make sense to us is the fact that you have this bonus but the computeTip method doesn't use it in calculating anything...

            So, we recommend that you change your computeTip to take the bonus because it is obvious that it is used in your "math" somehow.

            Your compute method should be something like:
            Code:
             double Tips::computeTip(double totalBill, double tipRate, double bonus)
            {
              //You now need to modify your math to 
              //match the business rules that dictate 
              //how to "compute the tip"
            
              //Obviously what you had before:
              //   return (totalBill + (totalBill * taxRate));
              //Is not correct because "bonus" isn't used
              //Figure out what is required to compute the tip 
              //taking "bonus" into consideration
              //and change your math accordingly
            
            }
            PS:

            In your main method you have while (running) but the variable running is never set to false and instead of allowing the while loop to actually loop you have a return 0; which ends app because this is main method (thankfully or else you would have an infinite loop)
            Last edited by Frinavale; Nov 12 '14, 05:19 PM.

            Comment

            Working...