Thread: Using STRUCT and FUNCTION

  1. #16
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    If you're going to copy and paste my code, at least read the comments I put in, then maybe remove them. O.o

  2. #17
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by noobkiddo View Post
    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?
    Ok, let me put it this way. Struct is a collection of variables. And you can also treat a struct like you treat a variable (mostly). Like Mostly Harmless said, you need to instantiate the struct and then make it as a parameter for your "displayArea()" function.

    For the function's definition is correct, however you're using it wrong. You didn't put any parameter at all into the function.

    Please re-read the links I gave you until you've understood the basics of functions and structs. And then I'm sure you can solve your problem yourself.

    BTW, your main() function is missing a return value.
    ERROR: Brain not found. Please insert a new brain!

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

  3. #18
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    did the problem statement needs a user to input its length and width??

    im already confused what to do Y_Y

  4. #19
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    i still don't get it even though i read and understands what it says, I'm really really tired to get the correct solution and syntax, I've tried everything still no luck for me, but thanks people for the time, tips and tuts, I appreciated you all. Anyway this is all I got, at least I tried my best. I dunno what will happen to me tomorrow. wish me luck guys.

  5. #20
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Ok, I'm gonna help you more. Consider this struct:

    Code:
    struct employee {
      int id_number;
      char[255] name;
      int age;
      float salary;
    };
    So an employee is an entity has other entities (or attributes if you will) like an id, name, age, and salary.

    And if I want to make an instance of employee named "noobkiddo" and insert the attributes in it, I'll make it like so:

    Code:
    int main()
    {
      struct employee noobkiddo; 
    
      strcpy(noobkiddo.name, "Noob Kiddo");
    
      noobkiddo.id_number = 1;
      noobkiddo.age = 18;
      noobkiddo.salary = 10000.00;
    
      return 0;
    }
    Now there's an employee instantiated as "noobkiddo" that has an id_number = 1, name = "Noob Kiddo", age = 18, and salary = 10000.00.

    For the function, like I said you've defined it right. However, you can't use it like this:
    Code:
    	cout << "Tdhe square feet of carpet is: " <<displayArea <<endl;
    A function needs a parameter(s). Even "function()" actually use a parameter, a NULL one. If you said that you've read it over & over but still can't understand the concept of it, I think you should read more.

    If you've understand the concept of
    ERROR: Brain not found. Please insert a new brain!

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

  6. #21
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by g4j31a5 View Post
    Code:
    struct employee {
      int id_number;
      char[255] name;
      int age;
      float salary;
    };
    Surely you mean:

    Code:
    struct employee
    {
         int id_number;
         char name[255];
         int age;
         float salary;
    };
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #22
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by IceDane View Post
    Surely you mean:

    Code:
    struct employee
    {
         int id_number;
         char name[255];
         int age;
         float salary;
    };
    Hahaha... Yeah sorry. My bad. :P
    ERROR: Brain not found. Please insert a new brain!

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

  8. #23
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    will this surely work??

    i dont want to try all over again, my brain is bleeding right now

    even my nose my ears my shoulder my head.. T_T until when do i suffer

    this maybe i should call my doctor and i have trauma bout coding...

    tnx g4j i'll try use ur given code

  9. #24
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    if i follow your example where should i put this:

    Code:
    float displayArea(float length, float width)
    {
    	float area;
    	area = length * width; 
    	return area;
    the computation thingy

  10. #25
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    will this work?

    Code:
    Carpet myCarpet;
    myCarpet.lengthInFeet=5.62;
    myCarpet.widthInFeet=2.21;

  11. #26
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Yes that's how you use a structure.

    Here's a generous example.

    Code:
    // <iostream> inscluded for std::cout
    #include <iostream>
    
    // Here we declare a structure, named rectangle
    // It has two elements: width and height (floats)
    struct rectangle {
    	float width;
    	float height;
    };
    
    // Here we declare our Area function
    // It returns a float, and takes a rectangle structure
    // as its only argument
    float Area(const struct rectangle& rect);
    
    int main(int argc, char* argv[]) {
    	// Create a variable, named myrect, of type rectangle
    	struct rectangle myrect;
    	
    	// Initialize the rectangles elements
    	myrect.width = 5.62;
    	myrect.height = 2.21;
    	
    	std::cout << "The rectangle's width is " << myrect.width << std::endl;
    	std::cout << "The rectangle's height is " << myrect.height << std::endl;
    	// Pass the rectangle to the Area() function
    	// It will multiply the rectangles width and height together
    	// and return the resulting float
    	std::cout << "The rectangle's area is " << Area(myrect) << std::endl;
    	
    	// main returns an integer
    	return 0;
    }
    
    // Here's the actual implementation of the function defined above	
    float Area(const struct rectangle& rect) {
    	return (rect.width * rect.height);
    }
    Note that const struct rectangle& rect might look a little foreign to you. Break it down:

    const: constant: the function will not alter the variable.
    struct: structure: the variable being passed is a structure.
    rectangle: the type of structure (user-defined)
    &: by reference: I'll let someone else explain this one.
    rect: the name of the variable for use in this specific function (user-defined).
    Last edited by nicoqwertyu; 04-15-2010 at 07:23 AM.

  12. #27
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    nicoqwertyu

    according to your example

    is that the format of the answer through my given problem statement?

    or is that what my problem statement will look like, the result?

  13. #28
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    now I understand a bit my only concern is

    what will be the outcome looks?

  14. #29
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Quote Originally Posted by noobkiddo View Post
    now I understand a bit my only concern is

    what will be the outcome looks?
    The outcome for every student will be different (very different); simply make sure you follow the guidelines given in the assignment. The other posters here have done a very nice job of breaking it down, into steps, for you.

  15. #30
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    will it be the same as your's outcome i mean look a like your's?

    is my problem statement needs a user to type the length and width

    when running the program?

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