Thread: calculating area of a circle using user-defined function

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    calculating area of a circle using user-defined function

    Hi

    I'm getting this error:
    error: void value not ignored as it ought to be

    Could you please tell me the reason for this? Do you find my code correct? Any suggestion? Please help me. Thanks.


    Code:
    // calculating area of a circle using user-defined function
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    void area(float);
    
    int main()
    
    {
        float r; float a;
    
    	cout << "enter radius: "; cin >> r;
    
    	a = area(r);
    
    	cout << a;
    
    	return 0;
    
    	system("pause");
    }
    
    //------------------------------------
    // area(int), function definition
    
            void area(float v)
    
    		{
    			float a;
    			a = 3.1416*(v*v);
    
    		}
    //--------------------------------------


    error: void value not ignored as it ought to be|
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well perhaps you should return a float, rather than void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Salem.

    The amended code is:

    Code:
    // calculating area of a circle using user-defined function
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    float area(float);
    
    int main()
    
    {
        float r; float a;
    
    	cout << "enter radius: "; cin >> r;
    
    	a = area(r);
    
    	cout << a;
    
    	return 0;
    
    	system("pause");
    }
    
    //------------------------------------
    // area(int), function definition
    
            float area(float v)
    
    		{
    			float Area;
    
    			Area = 3.1416*(v*v);
    
    			return Area;
    
    		}
    //--------------------------------------
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>error: void value not ignored as it ought to be|
    This simply means that the function you are calling has a return type of void but you are trying to assign the return value of that function to a variable.
    This obviously cannot be done since the function returns nothing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks, Elysia.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined cos function
    By zfite in forum C Programming
    Replies: 11
    Last Post: 04-03-2011, 02:40 AM
  2. Squareroot User Defined Function
    By Air in forum C Programming
    Replies: 2
    Last Post: 01-25-2009, 05:21 PM
  3. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  4. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM