Functions Error

This is a discussion on Functions Error within the C Programming forums, part of the General Programming Boards category; This program I wrote has an error. It would be nice if someone can trace the error in this. Write ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    15

    Functions Error

    This program I wrote has an error. It would be nice if someone can trace the error in this.


    Write a complete C Program that will either display [1]Area of the Circle OR [2]Area of the Square OR [3] Area of the Triangle. Use a function for each computation or task.


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    void main()
    void aCircle()
    void aSquare()
    void aTriangle()
    
    
    {
    	int rad, side, base, height, choice;
    	float A;
    	printf("[1]Area of the Circle\n");
    	printf("[2]Area of the Square\n");
    	printf("[3]Area of the Triangle\n");
    	scanf("%d", &choice);
    
    	switch(choice)
    	{
    		case 1: aCircle();
    					break;
    		case 2: aSquare();
    					break;
    		case 3: aTriangle();
    					break;
    		default: printf("Invalid Choice");
    	}
    
    		void aCircle()
    		{
    		int rad;
    		float A;
    		printf("Enter Radius of the Circle\n");
    		scanf("%d", &rad);
    		A=3.1416*rad*rad;
    		printf("Area of the Circle is %f", A);
    		}
    
    		void aSquare()
    		{
    			int side;
    			float A;
    			printf("Enter the Side of the Square\n");
    			scanf("%d", &side);
    			A=side*side;
    			printf("Area of the Square is %f", A);
    		}
    
    		void aTriangle()
    		{
    			int base, height;
    			float A;
    			printf("Enter the Base and Height\n");
    			scanf("%d %d", &base, &height);
    			A=(base*height)/2.0;
    			printf("Area of the Triangle %f", A);
    		}
    }

  2. #2
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Where's int main()?

    What are those undefined curly braces around everything?

    Why are your functions nested in them?

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by jhunluis View Post
    This program I wrote has an error. It would be nice if someone can trace the error in this.


    Write a complete C Program that will either display [1]Area of the Circle OR [2]Area of the Square OR [3] Area of the Triangle. Use a function for each computation or task.


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    void main()
    void aCircle()
    void aSquare()
    void aTriangle()
    
    
    {
    	int rad, side, base, height, choice;
    	float A;
    	printf("[1]Area of the Circle\n");
    	printf("[2]Area of the Square\n");
    	printf("[3]Area of the Triangle\n");
    	scanf("%d", &choice);
    
    	switch(choice)
    	{
    		case 1: aCircle();
    					break;
    		case 2: aSquare();
    					break;
    		case 3: aTriangle();
    					break;
    		default: printf("Invalid Choice");
    	}
    
    		void aCircle()
    		{
    		int rad;
    		float A;
    		printf("Enter Radius of the Circle\n");
    		scanf("%d", &rad);
    		A=3.1416*rad*rad;
    		printf("Area of the Circle is %f", A);
    		}
    
    		void aSquare()
    		{
    			int side;
    			float A;
    			printf("Enter the Side of the Square\n");
    			scanf("%d", &side);
    			A=side*side;
    			printf("Area of the Square is %f", A);
    		}
    
    		void aTriangle()
    		{
    			int base, height;
    			float A;
    			printf("Enter the Base and Height\n");
    			scanf("%d %d", &base, &height);
    			A=(base*height)/2.0;
    			printf("Area of the Triangle %f", A);
    		}
    }
    The prototype declaration should be done before main(). main() should properly return 0 at the end, by properly I mean it should have proper curly braces. Post a corrected piece of code.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The prototypes should also have a ; at the end of each one. In the future, try actually including the error messages you get.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Go back to your book and read it. That's not even close.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yep, a quick fix really isn't in order here - you just need to study the language a bit more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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