Getting Lost

Hey guys,

I am in an introductory class for C++ and we are doing a project to set up a monthly budget. This week the goal is to separate the code into different functions and I'm trying to figure out how to get the different functions to return the inputs to main, or the display.. I'm getting a little lost in the long strings of code. Is there anyone that can help me either to figure out how to get my inputs to return to the right place or just to organize my code so I don't get lost in it so easily...

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


int getIncome()
{
   float Income;

   cout << "\tYour monthly income: ";
   cin >> Income;

   return Income;
}


int getBudgetLiving()
{
   float Budget;

   cout << "\tYour budgeted living expenses: ";
   cin >> Budget;

   return Budget;
}


int getActualLiving()
{
   float Expenses;
   cout << "\tYour actual living expenses: ";
   cin >> Expenses;

   return Expenses;
}


int getDisplay()
{
   float Income;
   float Budget;
   float Taxes;
   float Offerings;
   float Other;
   float Expenses;

   //Set it to show the decimal points
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   //Display the inputs on a chart
   cout << "The following is a report on your monthly expenses\n";
   cout << "\tItem                  Budget          Actual\n";
   cout << "\t=============== =============== ===============\n";
   cout << "\tIncome          $" << setw(11) << Income << "    $"
        << setw(11) << Income << endl;

   cout << "\tTaxes           $" << setw(11) << 0.00 << "    $"
        << setw(11) << Taxes << endl;

   cout << "\tTithing         $" << setw(11) << 0.00 << "    $"
        << setw(11) << Offerings << endl;

   cout << "\tLiving          $" << setw(11) << Budget << "    $"
        << setw(11) << Expenses << endl;

   cout << "\tOther           $" << setw(11) << 0.00 << "    $"
        << setw(11) << Other << endl;

   cout << "\t=============== =============== ===============\n";
   cout << "\tDifference      $" << setw(11) << 0.00 << "    $"
        << setw(11) << 0.00 << endl;

   return 0;
}

int getActualOther()
{
   float Other;

   cout << "\tYour actual other expenses: ";
   cin >> Other;

   return Other;
}


int getActualTithing()
{
   float Offerings;
   cout << "\tYour actual tithe offerings: ";
   cin >> Offerings;

   return Offerings;
}


int getActualTax()
{
   float Taxes;

   cout << "\tYour actual taxes withheld: ";
   cin >> Taxes;

   return Taxes;
}

int main()
{


   //Explain what the program
   cout << "This program keeps track of your monthly budget\n";
   cout << "Please enter the following:\n";
   
   //Call for functions
   getIncome();

   getBudgetLiving();

   getActualLiving();

   getActualTax();

   getActualTithing();

   getActualOther();

   getDisplay();


   return 0;
}


Currently, this is what the output is when you run the code..

This program keeps track of your monthly budget
Please enter the following:
Your monthly income: 500
Your budgeted living expenses: 500
Your actual living expenses: 500
Your actual taxes withheld: 500
Your actual tithe offerings: 500
Your actual other expenses: 500
The following is a report on your monthly expenses
Item Budget Actual
=============== =============== ===============
Income $ 0.00 $ 0.00
Taxes $ 0.00 $ 0.00
Tithing $ 0.00 $ 0.00
Living $-119443384099143680.00 $ 0.00
Other $ 0.00 $ 0.00
=============== =============== ===============
Difference $ 0.00 $ 0.00
Last edited on
Line 6: getIncome is declared as returning an int, but you're returning a float. The result is going to be truncated. The return type should match what is being returned.

Line 17, 28, 78,89,99: ditto.

Line 40-45: These are uninitialized variables.

Line 57,60,63,66,69: You're displaying uninitialized variables.

Line 118-130: You're ignoring the return value from each of these functions.

All of you input routines are the same except for the prompt.

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

float getAmount (const string & prompt)
{   float value;

    cout << prompt; 
    cin >> value; 
    return value; 
}

void Display (float Income, float Budget, float Taxes, float Offerings, float Other, float Expenses)
{   //Set it to show the decimal points
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    //Display the inputs on a chart
    cout << "The following is a report on your monthly expenses\n";
    cout << "\tItem                  Budget          Actual\n";
    cout << "\t=============== =============== ===============\n";
    cout << "\tIncome          $" << setw(11) << Income << "    $"
        << setw(11) << Income << endl;
    cout << "\tTaxes           $" << setw(11) << 0.00 << "    $"
        << setw(11) << Taxes << endl;
    cout << "\tTithing         $" << setw(11) << 0.00 << "    $"
        << setw(11) << Offerings << endl;
    cout << "\tLiving          $" << setw(11) << Budget << "    $"
        << setw(11) << Expenses << endl;
   cout << "\tOther           $" << setw(11) << 0.00 << "    $"
        << setw(11) << Other << endl;
   cout << "\t=============== =============== ===============\n";
   cout << "\tDifference      $" << setw(11) << 0.00 << "    $"
        << setw(11) << 0.00 << endl;
}


int main()
{   float Income;
    float Budget;
    float Expenses;
    float Taxes;
    float Offerings;
    float Other;
    
    //Explain what the program
    cout << "This program keeps track of your monthly budget\n";
    cout << "Please enter the following:\n";
   
   //Call for functions
   Income = getAmount ("Enter your income"); 
   Budget = getAmount ("Enter budgeted living expenses"); 
   Expenses = getAmount ("Enter actual iving expenses"); 
   Taxes = getAmount ("Enter actual taxes");
   Offerings = getAmount ("Enter actual tithing");
   Other = getAmount ("Enter actual other"); 
   Display(Income,Budget,Taxes,Offerings,Other,Expenses);
   return 0;
}



Last edited on
One way would be to pass the values to the function getDisplay() after you have collected them in main, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int getDisplay(float Income, float Budget, float Taxes, float Offerings, float Other, float Expenses)
{
   // display here
}
int main()
{
   //Explain what the program
   cout << "This program keeps track of your monthly budget\n";
   cout << "Please enter the following:\n";
   
   //Call for functions
   float Income = getIncome();
   float Budget = getBudget();
   // and so on for the other vars

   getDisplay(Income, Budget, and other vars);

   return 0;
}
Thank you so much! Both of these comments helped me to better understand it.
Topic archived. No new replies allowed.