I had the right formula.
I had the right formula.
Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
Web Site: Zoo Crew
Forums: Zoo Boards
E-mail: [email protected]
"Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)
Here is your semi-fixed code:
You have a function that returns an floating point number but you never even assign that value in your main function. You also dynamically allocate your memory but who releases it? No one right now, might want to fix that or not use dynamic memory allocation.Code:#include <iostream.h> inline float GetArea(float *buffer, float radius) { buffer= new float[5]; buffer[2]= radius * radius; buffer[1]= 3.14; /* NOTE: May want more precision, try 3.14159 at least */ buffer[0]= buffer[2]*buffer[1]; return buffer[0]; } int main(int argc, char **argv) { float mybuffer, radius; float area; /* NEW */ cout << "Input the radius: "; cin >> radius; area = GetArea(&mybuffer, radius); /* Function returns something so deal with it */ cout << "The Area is: " << area; /* Output whatever the function returns */ cin.get(); return 0; }
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
retry the program again or try the one i did. its been fun but i have to go email me and let me know what happened [email protected]
Well the original example should be
Code:#include <iostream.h> inline void GetArea(float *buffer, float radius) { *buffer = radius * radius * 3.14; } int main(int argc, char **argv) { float mybuffer, radius; cout << "Input the radius: "; cin >> radius; GetArea(&mybuffer, radius); cout << "The Area is: " << buffer; cin.get(); return 0; }
I tried your code and it doesnt work
Perhaps
cout << "The Area is: " << mybuffer;
GetArea()
This function receives a pointer of type float.
It then allocates space for this pointer, giving it the space of 5 floats.
To do this, however, it should receive the address of the pointer
it's going to allocate :
This method is bad. First of all, it leaves the responsibility of deleting the memory to whoever calls GetArea()Code:inline float GetArea(float **buffer, float radius) { *buffer= new float[3]; (*buffer)[2]= radius * radius; (*buffer)[1]= 3.14; (*buffer)[0]= ( (*buffer)[2] )*( (*buffer)[1] ); return (*buffer)[0]; } // The parentheses are used for clarity ( hopefully )
2nd, it allocates memory when there's no need to. It can simply declare an array ( However, I'm assuming you're doing this in order to use pointers and dynamic mem allocation )
main
In main, you declared your mybuffer like this :
What you've done is declare an ordinary float ( not a pointer ) variable on the stack ( an auto variable, that is automatically deleted from memory when out of scope )Code:float mybuffer; . . . // And then you passed it to the function as this GetArea( &mybuffer,radius );
Then you passed the address of that variable to the function, as if it were an array, which is invalid.
A better way to do things ( yet using pointers ) :
Another way to do it ( yet using pointers ) :Code:int main(int argc, char **argv) { float *mybuffer, radius; cout << "Input the radius: "; cin >> radius; // Get area should be modified as above GetArea(&mybuffer, radius); cout << "The Area is: " << &mybuffer; // Never leave undeleted memory delete[] mybuffer; cin.get(); return 0; }
Code:inline float GetArea(float *buffer, float radius) { buffer[2]= radius * radius; buffer[1]= 3.14; buffer[0]= buffer[2] *buffer[1]; return buffer[0]; } int main( int argc, char**argv ) { float *buffer,radius; . . . buffer = new float[3]; GetArea( buffer,radius ); . . . delete[] buffer; . . . }
Last edited by Coder; 06-16-2002 at 02:40 PM.
Muhammad Haggag
thats a very interesting way to do it that i think works. clears things up for me
C++ Rules!!!!
------------
Microsoft Visual Studio .NET Enterprise