Hello and thanks in advance for your help. My program calculates the gratuity of various bills and gives the user how much to leave for a tip. If the user enters a negative amount for the tax, the default constructor is supposed to use 6.5%. My program compiles, but instead of subtracting the tax, it adds to it (when using a negative). What am I doing wrong?
Here is my code:

#include <iostream>
#include <iomanip>
using namespace std;

//Create the class
class Tips
{             
              
      public:
             double tar;
                           
             Tips()
             {if (tar < 0)
              tar = 6.5;
             }
                          
      double computeTip(double, double, double);
};

double Tips::computeTip(double t, double tir, double tar)
{
       
     double total = t;
     double tipRate = tir;
     double taxRate = tar;
     double meal;
     double tip;
               
     
           while (t != 999)
           {
                 cout << "Enter the total cost of the meal: ";     
                 cin  >> t;
                 if (t < 0)
                 {cout << "Invalid entry. Please enter a number greater than 0: ";
                  cin  >> t;
                 }                        
                 cout << "Enter the amount of tax: ";
                 cin  >> tar;                 
                 Tips::Tips();                                  
                 meal = (t / (tar + 100)) * 100;     
                 cout << "The total amount of the meal before tax is: "; 
                 cout << setprecision(4) << meal;
                 cout << endl;
                 cout << "Enter the tip percentage you would like to leave: ";
                 cin  >> tir; 
                 if (tir < 0)
                 {cout << "Invalid entry. Please enter a number greater than 0: ";
                  cin  >> tir;
                 }
                 tip = meal * (tir / 100);     
                 cout << "The tip rate you would like to leave is: " << tir / 100 << endl;
                 cout << "The amount of the tip you should leave is: " << setprecision(3) << tip << endl;   
                 cout << endl;
           }      
     
     return tip;
}

int main()
{
    Tips bill;
    
    double billRate, billMeal, billTir, billTip;
    
    cout << "This program computes the gratuity at a restaurant. Enter 999 to quit.";
    cout << endl;
    cout << endl;   
    bill.computeTip(billTir, billTip, billRate);    
    
    system("pause");
    return 0;
}

meal = (t / (tar + 100)) * 100; ???
Why not use meal = t * (tar +100)/100; ?

Returning to the problem asked:

The problem is that you have created a class that does nothing.
If you take the trouble to create a class don't ignore the values.
You can put tar=100000; and it will make ABSOLUTE NO difference to the code. I think you have confused yourself by using tar in the class AND tar in the function calculate tip. These are DIFFERENT varaibles

What you might like to do:

class Tip
 {
    private:

        const double taxRate;
        double totalTips;
        double totalBill;
   public:
       Tip(const double TR) : taxRate((TR>=0.0) ? TR : 6.50) {}

       void addBill(const double,const double);
};

void Tip::addTip(const double bill,const double tipRate)
{
  //Your code here -- check for 
  totatBill+=bill;
  totalTips+= // your code;
}

also you might like to note that (for a taxRate in percentage) tax=bill*taxRate/100.0; Finally, money is calculated to the nearest cent/penny, and some care is needed about double giving you 0.3333 etc.

Yes! Confused describes me perfectly right now! :-) How come your function has two different names? I think I pretty much had what you have, initially (I'll repost it). But how do I implement the constructor to validate the user input? No matter what I have or how I have it, the negative tax is added to the total. The negative tax should default to 6.5% and should be deducted from the total bill, then the total bill multiplied by tip rate to compute the tip amount.
Here was my initial code:

class Tips
{             
                          
      public:
                                       
             Tips()
             { 
              double taxRate = 6.5;
             }
                          
      double computeTip(double, double, double);
};

double Tips::computeTip(double t, double tir, double tar)
{
       
     double total = t;
     double tipRate = tir;
     double taxRate = tar;
     double meal;
     double tip;
               
     
           while (t != 999)
           {
                 cout << "Enter the total cost of the meal: ";     
                 cin  >> t;
                 if (t < 0)
                 {cout << "Invalid entry. Please enter a number greater than 0: ";
                  cin  >> t;
                 }                        
                 cout << "Enter the amount of tax: ";
                 cin  >> tar;                 
                 Tips::Tips();

Also, my meal variable calculates the tax as a percentage and I have setprecision to round the amounts to the penny.

Sorry the two different names, addTip and addBill was my mistake.
They should be the same. I just wrote it in the message window (not a very good excuse.)

Your problem is that you really haven't written a simple class yet and played with numbers.

consider

Tips(){  double taxRate = 6.5;  }

That does NOT set taxRate. It creates a local variable and then promptly gets rid of it.
So let use consider a simple class. Because without doing this you are never going to get you code correct.

class A
{
     int X;
 public:
     A() : X(11) {}       // set X to 11
     A(const int Y) { X=Y+21; }
     
     void setX(const int Z) { int X=Z; } // THIS DOES NOT SET X
     void setXReally(const int Z) { X=Z; } // THIS DOES SET X  
     void write() const { std::cout<<"X == "<<X<<std::endl; }

};

int main()
{
   A a1;
   a1.write();
   A a2(43);
   a2.write()
   a2.setX(432);
   a2.write();
   a2.setXReally(432);
   a2.write();
}

Now please go and EXPERIMENT with that code until you understand what it does.

p.s. you have to add standard headers. iostream etc.

Thanks StuXYZ! Got it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.