Help with Main/budget program

I've been having trouble with a budget program, the problem is that I keep calling a function more than once in main, I'm not sure how to fix this though. Any help would be appreciated.

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
#include <iostream>
#include <iomanip>
using namespace std;

//gets users income
float getIncome()
{
   float income;
   cout << "\tYour monthly income: ";
   cin >> income;
   return income;
}

//gets users budgeted living expenses
float getBudgetLiving()
{
   float budgetLiving;
   cout << "\tYour budgeted living expenses: ";
   cin >> budgetLiving;
   return budgetLiving;
}

//Gets users actual expenses
float getActualLiving()
{
   float actualLiving;
   cout << "\tYour actual living expenses: ";
   cin >> actualLiving;
   return actualLiving;
}

//gets users other expenses
float getActualOther()
{
   float other;
   cout << "\tYour actual other expenses: ";
   cin >> other;
   return other;
}

//computes tithing
float computeTithing()
{
   float tithe;
   float income = getIncome();
   tithe = income * 0.10;
   return tithe;
}

//Computes Tax
float computeTax(float a)
{
   float yearlyIncome;
   float yearlyTax;
   float monthlyTax;
   float income = getIncome();

   yearlyIncome = income * 12;

   if (a > 336550)
      yearlyTax = 91043 + 0.35*(yearlyIncome - 336550);
   else if (336550 >= a > 188450)
      yearlyTax = 42170 + 0.33*(yearlyIncome - 188450);
   else if (188450 >= a > 123700)
      yearlyTax = 24040 + 0.28*(yearlyIncome - 123700);
   else if (123770 >= a > 61300)
      yearlyTax = 8440 + 0.25*(yearlyIncome - 61300);
   else if (61300 >= a > 15100)
      yearlyTax = 15100 + 0.15*(yearlyIncome - 15100);
   else (15100 >= a > 0);
      yearlyTax = yearlyIncome*0.10;

   monthlyTax = yearlyTax/12;

   return monthlyTax;
}

//gets users actual tithe paid
float getActualTithing()
{
   float tithing;
   cout << "\tYour actual tithe offerings: ";
   cin >> tithing;
   return tithing;
}

//Gets users actual Tax Paid
float getActualTax()
{
   float tax;
   cout << "\tYour actual taxes withheld: ";
   cin >> tax;
   return tax;
}


float budgetDifference()
{
   float computeBudgetDifference;
   float income = getIncome();
   float budgetLiving = getBudgetLiving();
   float monthlyTax = computeTax(income);
   float other = getActualOther();
   float tithe = computeTithing();

   computeBudgetDifference = income - (budgetLiving+tithe+monthlyTax+other);
   return computeBudgetDifference;
}

float actualDifference()
{
   float computeActualDifference;
   float income = getIncome();
   float other = getActualOther();
   float tithing = computeTithing();
   float actualLiving = getActualLiving();
   float tax = getActualTax();

   computeActualDifference = income - (other+tithing+actualLiving+tax);

   return computeActualDifference;
}

//displayss budget Chart
float chartDisplay()
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   float income = getIncome();
   float budgetLiving = getBudgetLiving();
   float actualLiving = getActualLiving();
   float other = getActualOther();
   float tithing = getActualTithing();
   float tithe = computeTithing();
   float tax = getActualTax();
   float monthlyTax = computeTax(income);
   float computeBudgetDifference = budgetDifference();
   float computeActualDifference = actualDifference();

   cout << "\nThe following is a report on your monthly expenses" << endl;
   cout << "\tItem" << setw(24) << "Budget" << setw(16) << "Actual" << endl;
   cout << "\t=============== =============== ===============" << endl;
   cout << "\tIncome" << setw(11) << "$" << setw(11) << income << setw(5)
        << "$" << setw(11) << income << endl;
   cout << "\tTaxes" << setw(12) << "$" << setw(11) << tax << setw(5) << "$"
        << setw(11) << monthlyTax << endl;
   cout << "\tTithing" << setw(10) << "$" << setw(11) << tithe << setw(5)
        << "$" << setw(11) << tithing << endl;
   cout << "\tLiving" << setw(11) << "$" << setw(11) << budgetLiving << setw(5)
        << "$" << setw(11) << actualLiving << endl;
   cout << "\tOther" << setw(12) << "$" << setw(11) << other << setw(5)
        << "$" << setw(11) << other << endl;
   cout << "\t=============== =============== ===============" << endl;
   cout << "\tDifference" << setw(7) << "$" << setw(11)
        << computeBudgetDifference << setw(5) << "$" << setw(11)
        << computeActualDifference << endl;

   return 0;
}

//Main display
int main()
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   cout << "This program keeps track of your monthly budget\n"
           "Please enter the following:" << endl;
   cout << getActualOther()
        << getActualTithing()
        << getActualTax()
        << getActualLiving()
        << getBudgetLiving()
        << getIncome()
        << chartDisplay();

   return 0;
}
lets have a look at the getIncome() function. I see that function used in: main, computeTithing, computeTax, budgetDifference, actualDifference, chartDisplay. that's 6 times when you run the next code:
1
2
3
4
5
6
7
float getIncome()
{
   float income;
   cout << "\tYour monthly income: ";
   cin >> income;
   return income;
}


So, you're asking 6 times "your monthly income: ".

solution:
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
#include <iostream>
#include <iomanip>
using namespace std;

//gets users income
float getIncome()
{
	float income;
	cout << "\tYour monthly income: ";
	cin >> income;
	return income;
}

//gets users budgeted living expenses
float getBudgetLiving()
{
	float budgetLiving;
	cout << "\tYour budgeted living expenses: ";
	cin >> budgetLiving;
	return budgetLiving;
}

//Gets users actual expenses
float getActualLiving()
{
	float actualLiving;
	cout << "\tYour actual living expenses: ";
	cin >> actualLiving;
	return actualLiving;
}

//gets users other expenses
float getActualOther()
{
	float other;
	cout << "\tYour actual other expenses: ";
	cin >> other;
	return other;
}

//computes tithing
float computeTithing(float income)
{
	return income * 0.10;
}

//Computes Tax
float computeTax(float a)
{
	float yearlyIncome;
	float yearlyTax;
	float monthlyTax;

	yearlyIncome = a * 12;

	if (a > 336550)
		yearlyTax = 91043 + 0.35*(yearlyIncome - 336550);
	else if (336550 >= a > 188450)
		yearlyTax = 42170 + 0.33*(yearlyIncome - 188450);
	else if (188450 >= a > 123700)
		yearlyTax = 24040 + 0.28*(yearlyIncome - 123700);
	else if (123770 >= a > 61300)
		yearlyTax = 8440 + 0.25*(yearlyIncome - 61300);
	else if (61300 >= a > 15100)
		yearlyTax = 15100 + 0.15*(yearlyIncome - 15100);
	else (15100 >= a > 0);
	yearlyTax = yearlyIncome*0.10;

	monthlyTax = yearlyTax / 12;

	return monthlyTax;
}

//gets users actual tithe paid
float getActualTithing()
{
	float tithing;
	cout << "\tYour actual tithe offerings: ";
	cin >> tithing;
	return tithing;
}

//Gets users actual Tax Paid
float getActualTax()
{
	float tax;
	cout << "\tYour actual taxes withheld: ";
	cin >> tax;
	return tax;
}


float budgetDifference(float income, float budgetLiving, float other)
{
	float computeBudgetDifference;
	float monthlyTax = computeTax(income);
	float tithe = computeTithing(income);

	computeBudgetDifference = income - (budgetLiving + tithe + monthlyTax + other);
	return computeBudgetDifference;
}

float actualDifference(float income, float other, float actualLiving, float tax)
{
	float computeActualDifference;
	float tithing = computeTithing(income);

	computeActualDifference = income - (other + tithing + actualLiving + tax);

	return computeActualDifference;
}

//displayss budget Chart
float chartDisplay()
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	float income = getIncome();
	float budgetLiving = getBudgetLiving();
	float actualLiving = getActualLiving();
	float other = getActualOther();
	float tithing = getActualTithing();
	float tithe = computeTithing(income);
	float tax = getActualTax();
	float monthlyTax = computeTax(income);
	float computeBudgetDifference = budgetDifference(income, budgetLiving, other);
	float computeActualDifference = actualDifference(income, other, actualLiving, tax);

	cout << "\nThe following is a report on your monthly expenses" << endl;
	cout << "\tItem" << setw(24) << "Budget" << setw(16) << "Actual" << endl;
	cout << "\t=============== =============== ===============" << endl;
	cout << "\tIncome" << setw(11) << "$" << setw(11) << income << setw(5)
		<< "$" << setw(11) << income << endl;
	cout << "\tTaxes" << setw(12) << "$" << setw(11) << tax << setw(5) << "$"
		<< setw(11) << monthlyTax << endl;
	cout << "\tTithing" << setw(10) << "$" << setw(11) << tithe << setw(5)
		<< "$" << setw(11) << tithing << endl;
	cout << "\tLiving" << setw(11) << "$" << setw(11) << budgetLiving << setw(5)
		<< "$" << setw(11) << actualLiving << endl;
	cout << "\tOther" << setw(12) << "$" << setw(11) << other << setw(5)
		<< "$" << setw(11) << other << endl;
	cout << "\t=============== =============== ===============" << endl;
	cout << "\tDifference" << setw(7) << "$" << setw(11)
		<< computeBudgetDifference << setw(5) << "$" << setw(11)
		<< computeActualDifference << endl;

	return 0;
}

//Main display
int main()
{
	cout << "This program keeps track of your monthly budget\n"
		"Please enter the following:" << endl;
	chartDisplay();
	return 0;
}


And the finishing touch: before the "return 0;" in the main(), pause the screen with the code:
 
system ("PAUSE");
Oh! Thanks a lot!
closed account (zybCM4Gy)
Your problem is chartDisplay().

Having had to re-write the damn thing for my compiler not to flag up a hell of a lot of amount of warnings....

1. Prototype please
2. Check computeTax

do you really think that else if( 336550 >= a > 188450) is going to do what you think it's going to do? Try using the logical operators to make this more reader friendly.

After that within chartDisplay()

you ask for getIncome()

and in computeTax you also call for getIncome ....

These things aren't doing what you think they're doing.

Each time you call getIncome, you won't get the data out of the previous call, instead you'll be asked to enter new data.

...basically you're screwing with function calls a little too much.

--------------------------/rant

In main you will want an array of floats. You'll probably want to enumerate some names too...

So for...example/guide/though-provoke

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
array[6] = {}; //Empty array

enum chart_headings = { ActualOther, ActualTithing, ActualTax....}

//Input data into the array. 
array[ ActualOther ] = getActualOther
array[ActualTithin] = getActualTithing
 
/*********************/
// Compute tithing can use that information... 
computeTithing(array[Income]) //Remember it's been enumerated so this is effectively a number. 

float computeTithing(float income_from_array)
{
   float tithe;
  float income = income_from_array;
    tithe = income * 0.1; 
  return tithe
}


...To be honest I wouldn't even be using an array, I'd be using a struct.
Topic archived. No new replies allowed.