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)
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 usingnamespace.
#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.
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!
#include <iostream>
usingnamespace 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.