Just learning about structs and passing them to functions

This is as far as I have gotten. at this point, when I try to fix things, I just make them worse and forget how they were before, I thought it would be best to just ask for some help.

Can you guys please explain what the issues are in a way that makes sense (the compiler errors dont make sense to me)

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
#include <iostream>
using namespace std;
Dimensions getDimensions();
int calculateArea(int, int);
int calculateParameter(Dimensions);
void showRectangle(const Rectangle &);

struct Dimensions;
{
    int length;
    int width;
};

struct Rectangle
{
    int area;
    int parameter;
    Dimensions sizes;
};


int main()
{
    Rectangle shape;
    shape.sizes = getDimensions();
    
    calculateArea(shape.sizes.length, shape.sizes.width);
    shape.area = lTimesW;
    
    calculateParameter(shape.sizes);
    shape.parameter = addition;
    
    showRectangle(const Rectangle shape)
    
}

Dimensions getDimensions()
{
    Dimensions lAndW;
    cout << "Please enter the length of the shape: ";
    cin >> lAndW.length;
    cout << "Please enter the width of the shape: ";
    cin >> lAndW.width;
    
    return lAndW;
}

int calculateArea(length, width)
{
    int lTimesW = length * width;
    
    return lTimesW;
}

int calculateParameter(sizes)
{
    int addition = 2 * (sizes.length + sizes.width);
    
    return addition;
}

void showRectangle(const Rectangle &rect)
{
    cout << "The area of the rectangle is: " << rect.area << endl;
    cout << "The parameter of the rectangle is: " << rect.parameter;
}
What are the issues you are having, and what are you expecting your program to actually do?
Your function definitions at lines 48 and 55 are missing the data types for the parameters.

int calculateArea(int length, int width)

int calculateParameter(Dimensions sizes)

For a bit of review on function parameters:
https://www.learncpp.com/cpp-tutorial/introduction-to-function-parameters-and-arguments/
Last edited on
You are close. Remember, a struct is like any other thing — you can pass it to a function by value or by reference. Here it suits you to pass the shape by reference and modify it directly.

I have also taken the time to reformat your code a little. Prefer this structure over the antiquated structure you’ve had exampled to you so far.

Notice also how little grief it is to leave out the using namespace.

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
#include <iostream>

struct Dimensions
{
    int length;
    int width;
};

struct Rectangle
{
    int area;
    int perimeter;
    Dimensions sizes;
};


Dimensions getDimensions()
{
    Dimensions lAndW;
    std::cout << "Please enter the length of the shape: ";
    std::cin >> lAndW.length;
    std::cout << "Please enter the width of the shape: ";
    std::cin >> lAndW.width;
    
    return lAndW;
}


void calculateArea(Rectangle & rect)
{
    rect.area = rect.sizes.length * rect.sizes.width;
}


void calculatePerimeter(Rectangle & rect)
{
    rect.perimeter = 2 * (rect.sizes.length + rect.sizes.width);
}


void showRectangle(const Rectangle & rect)
{
    std::cout << "The area of the rectangle is: "      << rect.area      << "\n";
    std::cout << "The perimeter of the rectangle is: " << rect.perimeter << "\n";
}


int main()
{
    Rectangle shape;
    shape.sizes = getDimensions();
    
    calculateArea(shape);
    calculatePerimeter(shape);
    
    showRectangle(shape);
}
Please enter the length of the shape: 4
Please enter the width of the shape: 3
The area of the rectangle is: 12
The perimeter of the rectangle is: 14

I do not know what utility there is to have the dimensions as a separate structure — I would have just combined it with the Rectangle type, but this should give you good things to look at.

Hope this helps.
apart from things that do not exist in your sample but may elsewhere:
move the structs to the top. Also dimensions had a stray ; in yours.

you can't say
sometype foo(); //what is this?
and then
struct sometype... //too late, you tried to use it above, where there was no such thing.

there is a way to do that called a forward declaration, but its better if you don't have unnecessary ones (better to rearrange the code if you can).


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 Dimensions
{
    int length;
    int width;
};


struct Rectangle
{
    int area;
    int parameter;
    Dimensions sizes;
};


Dimensions getDimensions();
int calculateArea(int, int);
int calculateParameter(Dimensions);
void showRectangle(const Rectangle &);
Last edited on
Oh wow, it makes sense to put the structs above all the functions that use them instead of below- stupid mistake, thanks to everybody on that one. (so thats what the compiler was yelling at me about...)

also @dumthomas using the "namespace std" was the way we were taught since day one of our class. I dont understand the other way- why is it even important if they do the same thing? (just a question)

@George, thanks for the link I will check it out! always good to brush up on the basics! (even though im still learning all the basics XD)


also thank you so much @jonnin and @George and all the others for helping us noobs out, it really makes a huge difference and helps me understand topics better!

Last edited on
All fixed up now!!

This is exactly the way my teacher wanted it

she was very specific on how things were passed to functions so thats why I did it that way

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

struct Dimensions
{
    int length;
    int width;
};

struct Rectangle
{
    int area;
    int parameter;
    Dimensions sizes;
};


Dimensions getDimensions();
int calculateArea(int, int);
int calculateParameter(Dimensions);
void showRectangle(const Rectangle &);


int main()
{
    Rectangle shape;
    shape.sizes = getDimensions();
    
    
    shape.area = calculateArea(shape.sizes.length, shape.sizes.width);
    
    shape.parameter = calculateParameter(shape.sizes);
    
    showRectangle(shape);
    
}

Dimensions getDimensions()
{
    Dimensions lAndW;
    cout << "Please enter the length of the shape: ";
    cin >> lAndW.length;
    cout << "Please enter the width of the shape: ";
    cin >> lAndW.width;
    
    return lAndW;
}

int calculateArea(int length, int width)
{
    int lTimesW = length * width;
    
    return lTimesW;
}

int calculateParameter(Dimensions sizes)
{
    int addition = 2 * (sizes.length + sizes.width);
    
    return addition;
}

void showRectangle(const Rectangle &rect)
{
    cout << "The area of the rectangle is: " << rect.area << endl;
    cout << "The parameter of the rectangle is: " << rect.parameter;
}
Why is it even important if they do the same thing?

The problem is "A non-unique short name refers to an incorrect entity";
The solution is "Refer to each entity by its unique full name". It's a textbook application of root-cause analysis.

But this is a tiny program, with no third-party code, written by a single programmer, using a distinct naming convention. This software isn't likely to have the problem.

The solution does have a cost, although it is exceedingly small.
Similarly there is a risk of name collisions here, although it is also very small.
There is little to complain about here.
Topic archived. No new replies allowed.