Function call with a problem

Hi there :)

Happy Sunday!!! For this that are bored and can't wait to do some coding :D
I have question.
I have created this code to work out Cubic size of the room.
However, I have problem with function call, it highlights issue with first float variable. (for no good reason!! ;) yes I know reason is good my understanding is not (yet))

what do you think is an issue?

thanks :)

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
  // Struct-Volume.cpp : Defines the entry point for the console application.
//

#include <iostream>

struct Distance
{
	float feet;
	float inches;

};
struct Volume
{
	Distance width;
	Distance height;
	Distance depth;
};

float area(float, float, float, float, float, float);

int main()
{
	Volume v1;

	std::cout << "Please provide how deep is room" << std::endl;
	std::cout << "Feet: ";
	std::cin >> v1.depth.feet;
	std::cout << "Inches: ";
	std::cin >> v1.depth.inches;
	std::cout << std::endl;

	std::cout << "Please provide how wide is room" << std::endl;
	std::cout << "Feet: ";
	std::cin >> v1.width.feet;
	std::cout << "Inches: ";
	std::cin >> v1.width.inches;
	std::cout << std::endl;

	std::cout << "Please provide how high is room" << std::endl;
	std::cout << "Feet: ";
	std::cin >> v1.height.feet;
	std::cout << "Inches: ";
	std::cin >> v1.height.inches;
	std::cout << std::endl;

	float depth_Feet = v1.depth.feet;
	float depthInch = v1.depth.inches;
	
	float widthFeet = v1.width.feet;
	float widthInch = v1.width.inches;

	float heightFeet = v1.height.feet;
	float heightInches = v1.height.inches;

	float cubic = area(float depth_Feet, float depthInches, float widthFeet, float widthInches, float heightFeet, float heightInches);

	std::cout << "Qubic area of the room is: " << cubic << std::endl;

    return 0;
}
float area(float depthFeet_, float depthInches_, float widthFeet_, float widthInches_, float heightFeet_, float heightInches_)
{
	float cubicD = depthFeet_ + depthInches_ / 12;
	float cubicW = widthFeet_ + widthInches_ / 12;
	float cubicH = heightFeet_ + heightInches_ / 12;

	float cubic_ = cubicD * cubicW * cubicH;

	return cubic_;
}
Last edited on
Hello xxvms,

What stands out is the function call on line 55. Loose the "float" type before each variable. Only the variable name is needed here. The prototype, if used, and the function definition is where a type is needed. This may be of some help http://www.cplusplus.com/doc/tutorial/functions/

I will mention that "double"s are more accurate than"float"s.

I have not tested the program yet, but that should make a difference.

Hope that helps,

Andy

P.S. lines 55 and 57 have some misspelled variable names.
Last edited on
Hi Handy Andy!!

thanks :D I think I went blind!!! how I could not see this!!!

thank you :)
You can also simplify it a bit:
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
// Struct-Volume.cpp : Defines the entry point for the console application.
//

#include <iostream>

struct Distance {
    double feet;
    double inches;
};

struct Volume {
    Distance width;
    Distance height;
    Distance depth;
};

double area(const Volume&);

int main()
{
    Volume v1;

    std::cout << "Please provide how deep is room" << std::endl;
    std::cout << "Feet: ";
    std::cin >> v1.depth.feet;
    std::cout << "Inches: ";
    std::cin >> v1.depth.inches;
    std::cout << std::endl;

    std::cout << "Please provide how wide is room" << std::endl;
    std::cout << "Feet: ";
    std::cin >> v1.width.feet;
    std::cout << "Inches: ";
    std::cin >> v1.width.inches;
    std::cout << std::endl;

    std::cout << "Please provide how high is room" << std::endl;
    std::cout << "Feet: ";
    std::cin >> v1.height.feet;
    std::cout << "Inches: ";
    std::cin >> v1.height.inches;
    std::cout << std::endl;

    double cubic = area(v1);

    std::cout << "Qubic area of the room is: " << cubic << std::endl;

    return 0;
}

double area(const Volume& v)
{
    double cubicD = v.depth.feet + v.depth.inches / 12;
    double cubicW = v.width.feet + v.width.inches / 12;
    double cubicH = v.height.feet + v.height.inches / 12;

    double cubic = cubicD * cubicW * cubicH;

    return cubic;
}
Thank you Enoizat

that looks great :) I will keep in mind!!
this was exercise from book so I completed what was asked :)
Last edited on
Enoizat

I am reading your version of the code and it looks really neat!! Am I correct that this is passing by reference?

Last edited on
Am I correct that this is passing by reference?

Yes.


You also have a lot of code duplication that could be reduced with the use of a function or two. Perhaps something like:

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
...
#include <string>  // Required for the std::string class.
// Function prototype.
void get_dimensions(const std::string& message, Distance& value);

...

    // In main:
    Volume v1;

    get_dimensions("deep", v1.depth);
    get_dimensions("wide", v1.width);
    get_dimensions("high", v1.height);
...

// Function implementation:
void get_dimensions(const std::string& message, Distance& value)
{
    std::cout << "Please provide how " <<  message << " is room" << std::endl;
    std::cout << "Feet: ";
    std::cin >> value.feet;
    std::cout << "Inches: ";
    std::cin >> value.inches;
    std::cout << std::endl;
}
Last edited on
Hi Jlb,

thank you for that :)

I felt the same that there is so much code that is being repeated (my tutor usually says, if something appears twice in code think about loop) partly as my excuse this is early class and my class tutor didn't even want me to use functions only put all in main. We were covering struct with this exercise.

However, I am keen to learn how to deal with multiple objects that are created when for example user provides details (ie user wants to get value for 5 rooms, each separately and then summary of total size.

:)
once again thank you :)
Topic archived. No new replies allowed.