Whats wrong with my find Circle Area program?[compiles fine]

This is a discussion on Whats wrong with my find Circle Area program?[compiles fine] within the C++ Programming forums, part of the General Programming Boards category; I had the right formula....

  1. #16
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    I had the right formula.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is your semi-fixed code:
    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;
    }
    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.
    "...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

  3. #18
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    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 gamer4life687@yahoo.com

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    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;
    }

  5. #20
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I tried your code and it doesnt work

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Perhaps
    cout << "The Area is: " << mybuffer;

  7. #22
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    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 :
    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 )
    This method is bad. First of all, it leaves the responsibility of deleting the memory to whoever calls GetArea()
    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 :
    Code:
    float mybuffer;
    .
    .
    .
    // And then you passed it to the function as this
    GetArea( &mybuffer,radius );
    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 )
    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 ) :
    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;
    }
    Another way to do it ( yet using pointers ) :
    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

  8. #23
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    thats a very interesting way to do it that i think works. clears things up for me
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone find out what i'm doing wrong?
    By QuincyEQ in forum C Programming
    Replies: 2
    Last Post: 08-15-2002, 11:22 AM
  2. Whats wrong with my file streaming[it compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2002, 08:43 PM
  3. Help i cant find whats wrong with my code
    By 0927 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2001, 08:14 PM
  4. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 03:39 PM
  5. Help me find out what's wrong
    By biosx in forum C Programming
    Replies: 4
    Last Post: 08-15-2001, 12:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21