Structs, consts, and references

Hello forum, I'm new to posting here, but this site has been very helpful in teaching me C++. I've always had issues with structs and references, so could someone please enlighten me on how I would write the following function displayBudget(const MonthlyBudget&)? Would the values be set in the main individually or would they be set/displayed in the function? I've found tutorials on references, but I guess I need to hear it in this context :[

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

struct MonthlyBudget{
	double housing,
		utilities,
		medical,
                misc;

	MonthlyBudget(double rent=0.0, double util=0.0, double med=0.0,
				  double insur=0.0, double mis=0.0)
	{ housing=rent;
	  utilities=util;
	  medical=med;
	  misc=mis; }	
};

void displayBudget(const MonthlyBudget&);

int main(){
         return 0;
}


-Blue
Last edited on
The function takes a const reference to a MonthlyBudget. Given the name of "displayBudget", it stands to reason that the only thing the function would do is display/print the data contained within the budget. It would not modify the budget in any way.

That said, writing this function should be pretty straightforward.

1) give your MonthlyBudget reference a name so that you can access it.
2) you can access each individual member with the '.' dot operator. Just as you would with normal objects.
3) display each member using cout or whatever output method you're used to using.
Okay, thank you Disch, I figured it out. It really is simple, I'm probably just making it a lot harder on myself than I need to =) Below is what I got working, not sure if there's a better way of doing it atm, but this works and I'm happy it does.

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

struct MonthlyBudget{
	double housing,
		utilities,
		medical,
                misc;

	MonthlyBudget(double rent=0.0, double util=0.0, double med=0.0,
				  double insur=0.0, double mis=0.0)
	{ housing=rent;
	  utilities=util;
	  medical=med;
	  misc=mis; }	
}set;

void displayBudget(const MonthlyBudget&);

int main(){
	set.housing=500.00;
	set.utilities=150.00;
	set.medical=30.00;
	set.misc=50.00;
	displayBudget(set);
         return 0;
}

void displayBudget(const MonthlyBudget&){
	cout << "Housing\t\t" << set.housing << "\n";
	cout << "Utilities\t" << set.utilities << "\n";
	cout << "Medical\t\t" << set.medical << "\n";
	cout << "Miscellaneous\t" << set.misc << "\n";
}

*edited in 'set' at line 16, thanks for noticing, Disch*
Last edited on
You're kind of doing it wrong. For starters I don't see where 'set' is declared, but I'm assuming it's global.

If it's global, you are not using the passed parameter, which defeats the entire point of this exercise. You should give your parameter a name and use that name instead of 'set' in your function.
Oh, my fault. I have 'set' declared at the end of the struct MonthlyBudget, just didn't have it on the code I posted on here.

1
2
3
4
5
6
#include <iostream>
using namespace std;

struct MonthlyBudget{
 ...
} set;


That probably makes a slight bit more sense.
Yeah that makes a single global variable named set. That is not what you want.

You want to be able to pass any Budget to this function and have it printed. Example:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  // make two separate monthly budgets
  MonthlyBudget a(1,2,3,4,5);
  MonthlyBudget b(6,7,8,9,10);

  // display the 'a' budget
  displayBudget(a);

  // display the 'b' budget
  displayBudget(b);
}


displayBudget should give different output for each of those, because it's displaying two different budgets.

However your version will have the same output because it's printing the same, global, "set" budget, and not the budget that was passed to it as a paramter.
Okay, take three. Took out the global variable and put it in the main. Is this the correct way of doing this? It worked, but I'd like to ask regardless.

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

...

int main(){
        MonthlyBudget set(500.0, 150.0, 30.0, 50.0);
        MonthlyBudget set2;  //default constructor
	displayBudget(set);
        displayBudget(set2);
        return 0;
}

void displayBudget(const MonthlyBudget& a){
	cout << "Housing\t\t" << a.housing << "\n";
	cout << "Utilities\t" << a.utilities << "\n";
	cout << "Medical\t\t" << a.medical << "\n";
	cout << "Miscellaneous\t" << a.misc << "\n";
}
Last edited on
A+
If only you were my professor <.<
Topic archived. No new replies allowed.