struct - need a leeeetle help!

Hi folks,

I'm working my way through the learncpp.com tutorials and it's going well.
Now I'm on structs and i more or less understand them on a basic level.
One thing i can't get my head around is how to pass some struct parameters to a function that calculates a value and returns it. Below is a sketch i made. As you can see it's not right, but i can't work out what is the fundamental mistake i'm making. Any pointers would be truly welcome! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include "stdafx.h"
#include <iostream>

struct Earnings
{
	double HourlyRate;
	double HoursWorkPerWeek;
	};

Earnings CalculatePay(Earnings MrTom)
{
	return (MrTom.HourlyRate * MrTom.HoursWorkPerWeek);
}

int main()
{
	Earnings Tom{ 13.50, 32 };
	std::cout << CalculatePay(Tom) << "\n";
}
Last edited on
the only error is the type of return value of CalculatePay, it should be 'double', so:

double CalculatePay(Earnings MrTom)
Thank you sir!

So when you want a fuction to return a value, you should choose its type (is "type" the right word?) according to what it will return? So in this case, it must return a double, so it's type is a double?

So i guess you'd never return a double AND a string from the same fuction? (if you do, what "type" could you make it?!)

..........Daz
Last edited on
you either pass your required values by reference
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
, or create a small struct/class containing your double and string and have your function pass one of these back.
Last edited on
ahh, thanks, i'm actually only up to 4.7 on Learncpp.com, so i guess need to just plod on and will get there eventually!
or create a small struct/class containing your double and string and have your function pass one of these back

I've just realised you are indeed doing this, so it looks like you're on the right track. when do you do pass your object out of your function, you'll have to change line 18 to print out HourlyRate and HoursWorkPerWeek individually.

Line 17 looks a bit weird with the {} (and not () ), are you sure you want to do this? I could be wrong but that looks like array initialisation list notation.

Last edited on
double CalculatePay(Earnings MrTom)

The type of this function is not 'double'. The type of the return value is 'double'. The type of the return value is specified before the name of the function.

The type of this function is: double()(Earnings), which means it is a type of function that takes one value of type Earnings and returns a value of type double.
Kevin C, see OPs comment:
One thing i can't get my head around is how to pass some struct parameters to a function that calculates a value and returns it.


I could be wrong, but I think he/she does indeed want to return a struct.
calculates a value and returns it
Really? Especially seeing as the return statement is returning a value? Also curly braces for initialisation is actually a good idea because it is uniform initialisation which tends to make things easier.
Depends what he/she means by "it".
edit: I've been away from C++ too long I think. Maybe it's time for me to stop giving people wrong information and go away :/
Last edited on
Topic archived. No new replies allowed.