# Using STRUCT and FUNCTION

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-14-2010
noobkiddo
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
• 04-14-2010
vart
Quote:

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

I know
• 04-14-2010
nicoqwertyu
The book your instructor assigned knows.
• 04-14-2010
noobkiddo
Quote:

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?
• 04-14-2010
g4j31a5
• 04-14-2010
noobkiddo
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.
• 04-14-2010
Mostly Harmless
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.
• 04-14-2010
noobkiddo
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"; }```
• 04-14-2010
nicoqwertyu
You're missing some things.

Quote:

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>.
• 04-14-2010
noobkiddo
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()

Quote:

Carpet object is lenghtInFeet and widthInFeet
Fields? is it the same as carpet object?
Function is where length and width computes right?
• 04-14-2010
Mostly Harmless
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; }```
• 04-15-2010
Mostly Harmless
You beat me to my reply. ;-)

Quote:

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; }```
• 04-15-2010
noobkiddo
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?
• 04-15-2010
NeonBlack
Quote:

Originally Posted by noobkiddo
is this correct?

Compile it and run it.

Do you get the right answer?
• 04-15-2010
noobkiddo
yea i got the but in a form of something like this:

1004F01