Thread: Using STRUCT and FUNCTION

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    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. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by noobkiddo View Post
    Can anyone know how to answer this problem:
    I know
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    The book your instructor assigned knows.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Quote Originally Posted by vart View Post
    I know
    can u site an example of struct and function base on my given problem? so that i understand it?

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    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.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    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.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    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";
    }

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    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>.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    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?
    Last edited by noobkiddo; 04-14-2010 at 11:42 PM.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    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;
    }

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    You beat me to my reply. ;-)

    Quote Originally Posted by noobkiddo View Post
    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;
    }

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    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?

  14. #14
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by noobkiddo View Post
    is this correct?
    Compile it and run it.

    Do you get the right answer?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    yea i got the but in a form of something like this:

    1004F01

    is this the right answer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM