i put the numbers into my calculator and i receive a number that is about $1 greater than the actual value. why?

#include<iostream>
#ifndef Tips_H
#define Tips_H
using namespace std;

class Tips
{
private:
	long double taxRate,
		   bill,
		   gratuity;

	public:
	Tips()
	{
		taxRate = 0;
		bill = 0;
		gratuity = 0;
	}

	void setTaxRate(double t);
	void setBill(double b);
	void setGratuity(double g);
	void computeTip();

};
#endif

//Tips.cpp


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

void Tips::setTaxRate(double t)
{taxRate = t;if(taxRate == 0)taxRate = 0.065;if(taxRate<0){cout << "error! your tax can not be less then zero. pleaase restart program." <<endl;exit(0);}}

void Tips::setBill(double b)
{bill = b;if(bill<=0) {cout<<"Error! Your bill cannont be zero or less. Please re-start this program"<<endl; exit(0);}}
void Tips::setGratuity(double g)
{gratuity = g;if(gratuity<=0) {cout<<"Error! Your gratuity cannont be zero or less. Please re-start this program"<<endl; exit(0);}}

void Tips::computeTip()
{
double output;

output = ((taxRate * bill) + (bill) + (bill * gratuity));

cout << "You've entered: "<<endl;
cout << endl;
cout << fixed << showpoint << setprecision(2) << taxRate << endl;
cout << fixed << showpoint << setprecision(2) << bill << endl;
cout << fixed << showpoint << setprecision(2) << gratuity << endl;
cout << "Your total bill is $ "<< output <<endl;
cout << endl;
}

//main.cpp


#include<iostream>
using namespace std;

int main()
{
	long double Tax1,
		   Bill1,
		   Grat1;
		int choice;
		choice = 0;
		Tips Ti;
do
{
cout <<"Enter the following to calculate your total bill"<<endl;
cout << endl;

Tax1 = 0.065;
cout << "Tax Rate is "<< Tax1<<endl;
Ti.setTaxRate(Tax1);

cout << endl;
cout << "Enter your bill amount.  "<<endl;
cin   >>  Bill1;
Ti.setBill(Bill1);

cout <<endl;

cout << "Enter the Gratuity Rate You'd like to use (in decimal form)" <<endl;
cin >> Grat1;
cout << endl;

Ti.setGratuity(Grat1);
Ti.computeTip();




cout << "Would you like to calculate again? (Type 1 to repeat or 4 to quit) ";
cin  >> choice;
cout << endl;
cout << "-------------------------------------------------------"<<endl;
cout << endl;
}while(choice != 4);

}

Recommended Answers

All 6 Replies

What values are you entering when running the program?

the tax is always .065 (rounds to .07 in program) and enter your own numbers for gratuity and bill amount

Works fine for me. What numbers are you entering that make you believe it isn't?

I don't understand your source code. It doesn't make any sense.

Plus, I see the big error you made by bothering with OOP. It makes of little use in small programs.

I don't see anything wrong with the OOP concept being used. And in fact, it is recommended practice so that they can be reused in future.

Also, I do not see any error with the code. What's the number that caused the problem?

I'm not gonna point out some error but some guidelines in case you're interested.

#include <iostream>

class Tips {
   long double TaxRate,Bill,Gratuity;
      public :
         Tips ()
            : TaxRate(0.065),Bill(0),Gratuity(0) {} //initialization without executing body of the constructor

         Tips (int taxrate,int bill,int gratuity)
            : TaxRate(taxrate),Bill(bill),Gratuity(gratuity) {}
               //constructor makes mutators redundant in your case since you can initialize here

         ~Tips () {}

         long double ComputeTip ()  {
            if (TaxRate==0) TaxRate=0.065;
            //other conditions
            return (TaxRate*Bill) + Bill + (Bill*Gratuity);
          }
 };

int main ()  {
   long double taxrate,bill,gratuity;
   std::cin >> taxrate >> bill >> gratuity; //enter values
   Tips bill (taxrate,bill,gratuity);   //instantiation
   std::cout << bill.ComputeTip();
 }

Hope it's of help to you.

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.