Monthly budget project won't calculate numbers correctly

So for my C++ class we have to write code for a monthly budget chart, the user input info and the code spits out a chart. In 1 of the testBeds the code calculates the numbers and it's always .01 off.

Here are some screenshots of the tests. http://imgur.com/a/GC7gA

I can't figure out why it keeps happening, help?

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <iomanip>

using namespace std;

/**********************************************************************
*    Will ask user to input their monthly income.
 ***********************************************************************/
int getIncome(float &monthlyIncome)
{
   cout << "\tYour monthly income: ";
   cin >> monthlyIncome;
   
   return monthlyIncome;
}

/**********************************************************************
*    Will ask user for their taxes.
 ***********************************************************************/
int getActualTax(float &taxes)
{
   cout << "\tYour actual taxes withheld: ";
   cin >> taxes;
   
   return taxes;
}

/**********************************************************************
*    Will ask user for the tithing that they'll pay.
 ***********************************************************************/
int getActualTithing(float &tithe)
{
   cout << "\tYour actual tithe offerings: ";
   cin >> tithe;
   
   return tithe;
}

/**********************************************************************
*    Will ask user for their budgets living costs.
 ***********************************************************************/
int getBudgetLiving(float &bLivingExpenses)
{
   //b for budgedted
   cout << "\tYour budgeted living expenses: ";
   cin >> bLivingExpenses;
   
   return bLivingExpenses;
}

/**********************************************************************
*    Will ask user for their actual living costs.
 ***********************************************************************/
int getActualLiving(float &aLivingExpenses)
{
   //a for actual
   cout << "\tYour actual living expenses: ";
   cin >> aLivingExpenses;
   
   return aLivingExpenses;
}

/**********************************************************************
*    Will ask user for their actual other expenses.
 ***********************************************************************/
int getActualOTher(float &other)
{
   cout << "\tYour actual other expenses: ";
   cin >> other;
   
   return other;
}

/**********************************************************************
*    Will calculate taxes.
 ***********************************************************************/
float computeTax(float monthlyIncome)
{
   float yearlyTax; 
   float yearlyIncome = monthlyIncome * 12;
   
   if (yearlyIncome >= 0 && yearlyIncome < 15100)
      yearlyTax = (yearlyIncome * .10);
   else if (yearlyIncome >= 15100 && yearlyIncome < 61300)
      yearlyTax = 1510 + 0.15 * (yearlyIncome - 15100);
   else if (yearlyIncome >= 61300 && yearlyIncome < 123700)
      yearlyTax = 8440 + 0.25 * (yearlyIncome - 61300);
   else if (yearlyIncome >= 123700 && yearlyIncome < 188450)
      yearlyTax = 24040 + 0.28 * (yearlyIncome - 123700);
   else if (yearlyIncome >= 188450 && yearlyIncome < 336550)
      yearlyTax = 42170 + 0.33 * (yearlyIncome - 188450);
   else
      yearlyTax = 91043 + 0.35 * (yearlyIncome - 336550);
   
   float monthlyTax = (yearlyTax / 12);
   
   return monthlyTax;
}

/**********************************************************************
*    Will calculate their tithing.
 ***********************************************************************/
float computeTithing(float bTithe)
{  
   return (bTithe / 10);
}

/**********************************************************************
*    Will display a chart of the user's expenses.
 ***********************************************************************/
void display(float monthlyIncome, float taxes, float bTaxes, float tithe, 
   float bLivingExpenses, float aLivingExpenses, float other, float bTithe)
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);
   
   cout << "\n";
   
   cout << "The following is a report on your monthly expenses\n";
   
   cout << "\t" << setw(22) << left << "Item"
        << setw(16) << left << "Budget"
        << "Actual\n";
        
   cout << "\t=============== =============== ===============\n";
   
   cout << "\t" << setw(16) << left << "Income"
        << "$" << setw(11) << right << monthlyIncome << setw(5) << right
        << "$" << setw(11) << right << monthlyIncome << "\n";
        
   cout << "\t" << setw(16) << left << "Taxes"
        << "$" << setw(11) << right << bTaxes << setw(5) << right
        << "$" << setw(11) << right << taxes << "\n";
        
   cout << "\t" << setw(16) << left << "Tithing"
        << "$" << setw(11) << right << bTithe << setw(5) << right
        << "$" << setw(11) << right << tithe << "\n";
        
   cout << "\t" << setw(16) << left << "Living"
        << "$" << setw(11) << right << bLivingExpenses << setw(5) << right
        << "$" << setw(11) << right << aLivingExpenses << "\n";
        
   cout << "\t" << setw(16) << left << "Other"
        << "$" << setw(11) << right 
        << (monthlyIncome - bTaxes - bTithe - bLivingExpenses) 
        << setw(5) << right << "$" << setw(11) << right << other << "\n";
        
   cout << "\t=============== =============== ===============\n";
   
   cout << "\t" << setw(16) << left << "Difference"
        << "$" << setw(11) << right << 0.00 << setw(5) << right
        << "$" << setw(11) << right << (monthlyIncome - taxes - tithe -
        aLivingExpenses - other) << "\n";
}

/**********************************************************************
*    Will call all other functions.
 ***********************************************************************/
int main()
{  
   cout << "This program keeps track of your monthly budget\n"
        << "Please enter the following:\n";
   
   float income;
   float tax;
   float bTax; //budgeted Tax
   float tithing;
   float bTithing; //budgeted Tithing
   float budgetLiving;
   float actualLiving;
   float actualOther;
   
   getIncome(income);
   getBudgetLiving(budgetLiving);
   getActualLiving(actualLiving);
   getActualTax(tax);
   getActualTithing(tithing); 
   getActualOTher(actualOther);
   bTax = computeTax(income);
   bTithing = computeTithing(income);
        
   display(income, tax, bTax, tithing, budgetLiving, actualLiving,
   actualOther, bTithing);
   
   return 0;
}
closed account (48T7M4Gy)
Instead of wading through 187 lines of code and a very unclear depiction of your problem via the screen grabs, I suggest you calculate all money amounts in cents by converting dollars to cents, then perform the calculation, then convert back to dollars.

This is a very simple way of reducing rounding errors and there are benefits in using cents for later use with formatting currencies.

Also your balance sheets don't calculate in such a way that they balance. Adding up a column of figures and showing a zero 'difference' is wrong. You should at least have a third column so that the columns are:


       Estimated    Actual    Difference

TOTALS:ESTIMATE    ACTUAL    DIFFERENCE
closed account (48T7M4Gy)
Also,

Your functions are 'wrong' They return integers and yet the values used are floats Should be something like :)

1
2
3
4
5
6
7
8
9
10
11
/**********************************************************************
 *    Will ask user to input their monthly income.
 ***********************************************************************/
float getIncome()
{
    float income = 0;
    cout << "\tYour monthly income: ";
    cin >> income;
    
    return income;
}
Last edited on
Thanks for your input, I'll change them to floats instead of ints and see how it goes.
(Edit: Changing the functions to floats didn't change anything)

Also, I have to run my code through testBed through a program that my university gives us, the screenshots I posted is all that I see when I run the tests. And the columns and titles and stuff can't change. The text and numbers have to match what testBed wants exactly or else it will fail the code.

And I'll try converting to cents then back to dollars and see if that fixes it. Thank you again!
Last edited on
closed account (48T7M4Gy)
OK keep in touch. :)
closed account (48T7M4Gy)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <iomanip>

using namespace std;

/**********************************************************************
 *    Will ask user to input their monthly income.
 ***********************************************************************/
float getIncome()
{
    float income = 0;
    cout << "\tYour monthly income: ";
    cin >> income;
    
    return income;
}

/**********************************************************************
 *    Will ask user for their taxes.
 ***********************************************************************/
float getActualTax()
{
    float taxes = 0;
    cout << "\tYour actual taxes withheld: ";
    cin >> taxes;
    
    return taxes;
}

/**********************************************************************
 *    Will ask user for the tithing that they'll pay.
 ***********************************************************************/
float getActualTithing()
{
    float tithe = 0;
    cout << "\tYour actual tithe offerings: ";
    cin >> tithe;
    
    return tithe;
}

/**********************************************************************
 *    Will ask user for their budgets living costs.
 ***********************************************************************/
float getBudgetLiving()
{
    float bLivingExpenses = 0;
    //b for budgedted
    cout << "\tYour budgeted living expenses: ";
    cin >> bLivingExpenses;
    
    return bLivingExpenses;
}

/**********************************************************************
 *    Will ask user for their actual living costs.
 ***********************************************************************/
float getActualLiving()
{
    float aLivingExpenses = 0;
    //a for actual
    cout << "\tYour actual living expenses: ";
    cin >> aLivingExpenses;
    
    return aLivingExpenses;
}

/**********************************************************************
 *    Will ask user for their actual other expenses.
 ***********************************************************************/
float getActualOTher()
{
    float other = 0;
    cout << "\tYour actual other expenses: ";
    cin >> other;
    
    return other;
}

/**********************************************************************
 *    Will calculate taxes.
 ***********************************************************************/
float computeTax(float monthlyIncome)
{
    float yearlyTax;
    float yearlyIncome = monthlyIncome * 12;
    
    if (yearlyIncome >= 0 && yearlyIncome < 15100)
        yearlyTax = (yearlyIncome * .10);
    else if (yearlyIncome >= 15100 && yearlyIncome < 61300)
        yearlyTax = 1510 + 0.15 * (yearlyIncome - 15100);
    else if (yearlyIncome >= 61300 && yearlyIncome < 123700)
        yearlyTax = 8440 + 0.25 * (yearlyIncome - 61300);
    else if (yearlyIncome >= 123700 && yearlyIncome < 188450)
        yearlyTax = 24040 + 0.28 * (yearlyIncome - 123700);
    else if (yearlyIncome >= 188450 && yearlyIncome < 336550)
        yearlyTax = 42170 + 0.33 * (yearlyIncome - 188450);
    else
        yearlyTax = 91043 + 0.35 * (yearlyIncome - 336550);
    
    float monthlyTax = (yearlyTax / 12);
    monthlyTax = int(monthlyTax * 100)/100; // <--- Or use round() function
    
    return monthlyTax;
}

/**********************************************************************
 *    Will calculate their tithing.
 ***********************************************************************/
float computeTithing(float income)
{
    return (int)(income * 100) / 1000;
}

/**********************************************************************
 *    Will display a chart of the user's expenses.
 ***********************************************************************/
void display(float monthlyIncome, float taxes, float bTaxes, float tithe,
             float bLivingExpenses, float aLivingExpenses, float other, float bTithe)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    cout << "\n";
    
    cout << "The following is a report on your monthly expenses\n";
    
    cout << "\t" << setw(22) << left << "Item"
    << setw(16) << left << "Budget"
    << "Actual\n";
    
    cout << "\t=============== =============== ===============\n";
    
    cout << "\t" << setw(16) << left << "Income"
    << "$" << setw(11) << right << monthlyIncome << setw(5) << right
    << "$" << setw(11) << right << monthlyIncome << "\n";
    
    cout << "\t" << setw(16) << left << "Taxes"
    << "$" << setw(11) << right << bTaxes << setw(5) << right
    << "$" << setw(11) << right << taxes << "\n";
    
    cout << "\t" << setw(16) << left << "Tithing"
    << "$" << setw(11) << right << bTithe << setw(5) << right
    << "$" << setw(11) << right << tithe << "\n";
    
    cout << "\t" << setw(16) << left << "Living"
    << "$" << setw(11) << right << bLivingExpenses << setw(5) << right
    << "$" << setw(11) << right << aLivingExpenses << "\n";
    
    cout << "\t" << setw(16) << left << "Other"
    << "$" << setw(11) << right
    << (monthlyIncome - bTaxes - bTithe - bLivingExpenses)
    << setw(5) << right << "$" << setw(11) << right << other << "\n";
    
    cout << "\t=============== =============== ===============\n";
    
    cout << "\t" << setw(16) << left << "TOTALS"
    << "$" << setw(11) << right << 0.00 << setw(5) << right
    << "$" << setw(11) << right << (monthlyIncome - taxes - tithe -
                                    aLivingExpenses - other) << "\n";
}

/**********************************************************************
 *    Will call all other functions.
 ***********************************************************************/
int main()
{
    cout
    << "This program keeps track of your monthly budget\n"
    << "Please enter the following:\n";
    
    float income = getIncome();
    
    float bTax = computeTax(income);
    float tax = getActualTax();
    
    float bTithing = computeTithing(income);
    float tithing = getActualTithing();
    
    float budgetLiving = getBudgetLiving();
    float actualLiving = getActualLiving();
    
    float actualOther =  getActualOTher();
    
    display(income, tax, bTax, tithing, budgetLiving, actualLiving,
            actualOther, bTithing);
    
    return 0;
}


This program keeps track of your monthly budget
Please enter the following:
	Your monthly income: 1234.56
	Your actual taxes withheld: 98.72
	Your actual tithe offerings: 245.37
	Your budgeted living expenses: 457.83
	Your actual living expenses: 562.19
	Your actual other expenses: 98.77

The following is a report on your monthly expenses
	Item                  Budget          Actual
	=============== =============== ===============
	Income          $    1234.56    $    1234.56
	Taxes           $     123.00    $      98.72
	Tithing         $     123.00    $     245.37
	Living          $     457.83    $     562.19
	Other           $     530.73    $      98.77
	=============== =============== ===============
	TOTALS          $       0.00    $     229.51
Program ended with exit code: 0
Topic archived. No new replies allowed.