If you're going to copy and paste my code, at least read the comments I put in, then maybe remove them. O.o
If you're going to copy and paste my code, at least read the comments I put in, then maybe remove them. O.o
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.
did the problem statement needs a user to input its length and width??
im already confused what to do Y_Y
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.
Ok, I'm gonna help you more. Consider this struct:
So an employee is an entity has other entities (or attributes if you will) like an id, name, age, and salary.Code:struct employee { int id_number; char[255] name; int age; float 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:
Now there's an employee instantiated as "noobkiddo" that has an id_number = 1, name = "Noob Kiddo", age = 18, and salary = 10000.00.Code:int main() { struct employee noobkiddo; strcpy(noobkiddo.name, "Noob Kiddo"); noobkiddo.id_number = 1; noobkiddo.age = 18; noobkiddo.salary = 10000.00; return 0; }
For the function, like I said you've defined it right. However, you can't use it like this:
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.Code:cout << "Tdhe square feet of carpet is: " <<displayArea <<endl;
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.
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
if i follow your example where should i put this:
the computation thingyCode:float displayArea(float length, float width) { float area; area = length * width; return area;
will this work?
Code:Carpet myCarpet; myCarpet.lengthInFeet=5.62; myCarpet.widthInFeet=2.21;
Yes that's how you use a structure.
Here's a generous example.
Note that const struct rectangle& rect might look a little foreign to you. Break it down: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); }
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.
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?
now I understand a bit my only concern is
what will be the outcome looks?
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?