Using STRUCT and FUNCTION

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 04-15-2010
Mostly Harmless
If you're going to copy and paste my code, at least read the comments I put in, then maybe remove them. O.o
• 04-15-2010
g4j31a5
Quote:

Originally Posted by 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?

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.
• 04-15-2010
noobkiddo
did the problem statement needs a user to input its length and width??

im already confused what to do Y_Y
• 04-15-2010
noobkiddo
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.
• 04-15-2010
g4j31a5

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
• 04-15-2010
IceDane
Quote:

Originally Posted by g4j31a5
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; };```
• 04-15-2010
g4j31a5
Quote:

Originally Posted by IceDane
Surely you mean:

Code:

```struct employee {     int id_number;     char name[255];     int age;     float salary; };```

Hahaha... Yeah sorry. My bad. :P
• 04-15-2010
noobkiddo
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
• 04-15-2010
noobkiddo

Code:

```float displayArea(float length, float width) {         float area;         area = length * width;         return area;```
the computation thingy
• 04-15-2010
noobkiddo
will this work?

Code:

```Carpet myCarpet; myCarpet.lengthInFeet=5.62; myCarpet.widthInFeet=2.21;```
• 04-15-2010
nicoqwertyu
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).
• 04-15-2010
noobkiddo
nicoqwertyu

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

or is that what my problem statement will look like, the result?
• 04-15-2010
noobkiddo
now I understand a bit my only concern is

what will be the outcome looks?
• 04-15-2010
nicoqwertyu
Quote:

Originally Posted by noobkiddo
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.
• 04-15-2010
noobkiddo
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?
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last