Program bug (Tip/Gratuity Calculator)
Feb 8, 2011 at 5:23am UTC
Hi, I 98% finished this program that is suppose to calculate the gratuity from a bill. The only problem I'm having is the do-while loop. On the first run of the program, it calculates correctly and displays correctly, but on the second or continuous runs, the program would use the same gratuity rate as the first run.
The program uses 3 different files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
//Tips.h
#include<iostream>
#ifndef Tips_H
#define Tips_H
using namespace std;
class Tips
{
private :
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 "Tips.h"
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
void Tips::setTaxRate(double t)
{taxRate = t;if (taxRate == 0)taxRate = 0.65;}
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 $ " << fixed << showpoint << setprecision(2) << output <<endl;
cout << endl;
}
//main.cpp
#include "Tips.h"
#include<iostream>
using namespace std;
int main()
{
double Tax1,
Bill1,
Grat1;
int choice;
choice = 0;
Tips Ti;
do
{
cout <<"Enter the following to calculate your total bill" <<endl;
cout << endl;
cout << "Enter your tax rate. *If none enter zero. " <<endl;
cin >> Tax1;
Ti.setTaxRate(Tax1);
cout << endl;
cout << "Enter your bill amount. " <<endl;
cin >> Bill1;
Ti.setBill(Bill1);
cout <<endl;
cout << "Choose the gratuity rate of your choice" <<endl;
cout << "1. 15% --> Epic Fail" <<endl;
cout << "2. 18% --> Good Job!" <<endl;
cout << "3. 20% --> Awesome!!" <<endl;
cout << "4. Quit the Program!" <<endl;
cout << endl;
while ((choice < 1) || (choice > 4))
{
cout << "Please enter either 1, 2, 3, or 4: " ;
cin >> choice;
if (choice == 1)
Grat1 = 0.15;
else if (choice == 2)
Grat1 = 0.18;
else if (choice == 3)
Grat1 = 0.20;
else if (choice == 4)
exit(0);
}
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);
}
Many of these were used from the text book as references, but if there is any error please let me know. Thank you.
Last edited on Feb 8, 2011 at 5:30pm UTC
Feb 8, 2011 at 6:13am UTC
Line 96 will only enter the while loop if your choice value is invalid. It's still valid from the previous run through the loop since you haven't reset it, so it skips over it.
You ought to fix your indentation as well. It would make things much easier to read.
Feb 8, 2011 at 5:33pm UTC
Thanks Zhuge. I see what you mean, so how can I reset the loop? Also, what line needs indentation?
Feb 8, 2011 at 10:13pm UTC
Well I fixed the problem by changing the program a little. I removed the do-while loop and added go-to to the locations it should loop from and to. Thanks.
Topic archived. No new replies allowed.