# Thread: Using STRUCT and FUNCTION

1. ## Using STRUCT and FUNCTION

Can anyone know how to answer this problem:

Create a structure named Carpet that has two public data members: lengthInFeet and widthInFeet. Write a main() function that instantiates a Carpet object and assigns values to its fileds. Pass the object to a function named Area() that calculates the carpet area in square feet and displays the results. Save the file as Carpet.cpp

2. Originally Posted by noobkiddo
Can anyone know how to answer this problem:
I know

3. The book your instructor assigned knows.

4. Originally Posted by vart
I know
can u site an example of struct and function base on my given problem? so that i understand it?

5. what i mean is how and where do i start answering the problem statement?

can anyone create the beginning part and I will try to continue and hopefully finish it.

6. The links g4j31a5 are what you need. That's how and where you start.

Break down the assignment into steps:

1) Create a structure named Carpet
2) that has two public data members: lengthInFeet and widthInFeet.
3) Write a main() function
4) that instantiates a Carpet object
5) and assigns values to its fileds.
6) Pass the object to a function named Area()
7) that calculates the carpet area in square fee
8) and displays the results
9) Save the file as Carpet.cpp

The links posted should give you what you need to perform the majority of these steps.

7. is my statement correct?

Code:
```#include <stdio.h>
using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

int multiplication (int l, int w)
{
int c;
c=l*w;
return (c);

int main()
{
int l, w;
cout<<"Enter length:";
cin<<"l";
cout<<"Enter width:";
cin>>"w";
cout<<"The square feet of carpet is: <<c";
}```

8. You're missing some things.

The links g4j31a5 are what you need. That's how and where you start.

Break down the assignment into steps:

1) Create a structure named Carpet
2) that has two public data members: lengthInFeet and widthInFeet.
3) Write a main() function
4) that instantiates a Carpet object
5) and assigns values to its fileds.
6) Pass the object to a function named Area()
7) that calculates the carpet area in square fee
8) and displays the results
9) Save the file as Carpet.cpp
Also, you're including stdio.h, but using cout; replace <stdio.h> with <iostream>.

9. Code:
```#include <iostream>
using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

float Area (float l, float w)
{
int c;
c=l*w;
return (c);
}

int main()
{

int l, w;
cout<<"Enter length:";
cin>>l;
cout<<"Enter width:";
cin>>w;
cout<<"The square feet of carpet is:" <<Area(l,w) <<endl;
return 0;
}```
my program is already running but how can I do:

4) that instantiates a Carpet object
5) and assigns values to its fileds.
6) Pass the object to a function named Area()

Carpet object is lenghtInFeet and widthInFeet
Fields? is it the same as carpet object?
Function is where length and width computes right?

10. It's a start. A few general things before I get to the problem at hand:
• #include <iostream>, not <stdio.h>. stdio.h is the C standard I/O library.
• You forgot a closing bracket at the end of the multiplication() function.
• For readability, use descriptive variable names, like width instead of w.
• Use << for cout and >> for cin.
• main() always returns an int.
• Don't put variable names in double quotes.

As far as how it adheres to specs, a few things:
• You still need to create a Carpet object in main() before you can use it.
• After you read in the length and width, you need to assign those values to the Carpet object.
• Your multiplication() function should be called Area() per the specs.
• length and width are floats, not ints.
• The Area() function needs to take an object of type Carpet, not two ints.
• The Area() function needs to return a float, since float * float = float.

Code:
```#include <iostream>

using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

float Area(/*Carpet object goes here*/)
{
float area;
area = length * width; //need to access lengthInFeet and widthInFeet from Carpet object.
return area;
}

int main()
{
float length, width; //length and width are floats
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
cout << "The square feet of carpet is: " << c;
}```

11. You beat me to my reply. ;-)

Originally Posted by noobkiddo
my program is already running but how can I do:

4) that instantiates a Carpet object
5) and assigns values to its fileds.
6) Pass the object to a function named Area()
To instantiate and object means to create an instance of that object. That is, you can't use Carpet directly. Think of Carpet as any other data type, like an int or float. You can't do:
Code:
```int = 5
float = 3.14```
It's the same for structs. You need to create an object of type Carpet:
Code:
```Carpet myCarpet;
Carpet persianRug;```
Once you have a Carpet object, you can do things to it. In your Carpet struct, you have two data fields, lengthInFeet and widthInFeet. To use them, you use the dot operator:
Code:
```Carpet myCarpet;

myCarpet.lengthInFeet = 11.0;
myCarpet.widthInFeet = 7.5;

cout << "Length in feet: " << myCarpet.lengthInFeet << endl;
cout << "Width in feet: " << myCarpet.widthInFeet << endl;```
Lastly, you pass a struct like any other variable:
Code:
```float Area(Carpet myCarpet)
{
//stuff
}

int main()
{
Carpet persianRug;

//stuff

cout << "Area of carpet: " << Area(persianRug);

return 0;
}```

12. Code:
```#include <iostream>

using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

float displayArea(float length, float width)
{
float area;
area = length * width; //need to access lengthInFeet and widthInFeet from Carpet object.
return area;
}

int main()
{
float length, width; //length and width are floats
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
cout << "The square feet of carpet is: " <<displayArea <<endl;
}```
is this correct?

13. Originally Posted by noobkiddo
is this correct?
Compile it and run it.

Do you get the right answer?

14. yea i got the but in a form of something like this:

1004F01