how to make functions?

This is a discussion on how to make functions? within the C Programming forums, part of the General Programming Boards category; I need to write a program that calculates the area and perimeters of geometric shapes my question is how do ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    how to make functions?

    I need to write a program that calculates the area and perimeters of geometric shapes my question is how do i make it into different functions and in a loop until the user exits the program? if the user enters a none interger or float how should i prompt an error message?
    thank you for you time
    clear

    #include <stdio.h>

    void main (){

    int chose = 0;
    int rwidth = 0;
    int rheight = 0;
    int rarea = 0;
    int rperi = 0;
    int swidth = 0;
    int sheight = 0;
    int sarea = 0;
    int speri = 0;
    int radius = 0;
    int carea = 0;
    int cperi = 0;
    float pi =3.14;
    int base =0;
    int rside = 0;
    int lside = 0;
    int tarea = 0;
    int tperi =0;

    printf("Please choose 1-5 to calculate\n");
    printf("the area and perimeter/circumference\n");
    printf("of the various geometric shapes.\n");

    printf("1. Rectangle\n");
    printf("2. Square\n");
    printf("3. Circle\n");
    printf("4. Triangle\n");
    printf("5. Exit (or EOF, Control+Z)\n");

    scanf("%d", &chose);

    if (chose == 1){
    printf("Please enter the width\n");
    scanf("%d", &rwidth);
    printf("Please enter the height\n");
    scanf("%d",&rheight);

    rarea = rwidth * rheight;
    rperi = (rwidth * rheight) * 2;

    printf("The area of the rectangle is %d\n", rarea);
    printf("The perimeter of the rectangle is %d\n", rperi);
    }
    else if (chose == 2){
    printf("Please enter the width\n");
    scanf("%d", &swidth);
    printf("Please enter the height\n");
    scanf("%d",&sheight);

    sarea = swidth * sheight;
    speri = (swidth * sheight);

    printf("The area of the square is %d\n", sarea);
    printf("The perimeter of the square is %d\n", speri);
    }
    else if (chose == 3){
    printf("Please enter the radius\n");
    scanf("%d", &radius);

    carea = pi * radius * radius;
    cperi = 2 * pi * radius;

    printf("The area of the cirlce is %d\n", carea);
    printf("The circumference of the circle is %d\n", cperi);
    }
    else if (chose == 4){

    printf("Please enter the base\n");
    scanf("%d",&base);
    printf("Pleare enter the left side\n");
    scanf("%d", &lside);
    printf("Please enter the right side\n");
    scanf("%d",&rside);

    tperi = base + lside + rside;
    tarea = (base * lside) / 2;

    printf("The area of the triangle is %d\n", tarea);
    printf("The perimeter of the triangle is %d\n", tperi);
    }
    else if (chose == 5) {

    EOF;



    }


    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    1) Don't use void main(). Use int main().
    2) Use code tags (information on the top of every board).
    3) EOF; ?? What's this?
    4) Instead of that big if, it's better style to use a switch in this case.
    5) Look up scanf() in your documentation or on google. Here's a hint (correct me if I'm wrong, I rarely do C):
    Code:
    if (scanf("%d", num) != 1)
       printf("Invalid input!\n");

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    another problem

    speedy 5 your suggestion of use the switch statement was great. thank you
    i hadn't thought about it because it hasn't been tought in my class yet.
    as for EOF it means end of file and exits the program.
    now i got mostly everything down i still have one problem
    im suppose to prompt an error message when the user doesnt give a interger when asking for the height, width, side ....etc...
    what should i use?
    and when i enter things other than intergers the program would just keep looping forever.
    one sample output i need is when the use enters triangle and gets an error message and is prompt again
    enter again. what do i need for it statement do i need for it to do that and stop looping?
    thanks a million
    clear
    Code:
    #include <stdio.h>
    #define PI 3.14
    
    void main (){
    	
    	int chose = 0;
    	int rwidth = 0;
    	int rheight = 0;
    	int rarea = 0;
    	int rperi = 0;
    	int side = 0;
    	int sarea = 0;
    	int speri = 0;
    	int radius = 0;
    	double carea = 0;
    	double cperi = 0;
    	int base =0;
    	int rside = 0;
    	int lside = 0;
    	int tarea = 0;
    	int tperi =0;
    	int theight = 0;
    	
    
    	printf("Please choose 1-5 to calculate\n");
    	printf("the area and perimeter/circumference\n"); 
    	printf("of the various geometric shapes.\n");
    	do {
    	printf("1. Rectangle\n");
    	printf("2. Square\n");
    	printf("3. Circle\n");
    	printf("4. Triangle\n");
    	printf("5. Exit (or EOF, Control+Z)\n");
    
    	printf("Enter a value between 1 and 5: ");
    
    		scanf("%d", &chose);
    
    		switch (chose)
    		{
    		case (1) :	printf("Please enter the width: ");
    					scanf("%d", &rwidth);
    					printf("Please enter the height: ");
    					scanf("%d",&rheight);
    		
    					rarea = rwidth * rheight;
    					rperi = 2 * (rwidth * rheight);
    	
    					printf("The area of the rectangle is %d.\n", rarea);
    					printf("The perimeter of the rectangle is %d.\n", rperi);
    					break;
    		case (2) :  printf("Please enter the side: ");
    					scanf("%d", &side);
    						
    					sarea = side * 4;
    					speri = (side * side);
    			
    					printf("The area of the square is %d.\n", sarea);
    					printf("The perimeter of the square is %d.\n", speri);
    					break;
    		case (3) :  printf("Please enter the radius: ");
    					scanf("%d", &radius);
    				
    					carea = PI * radius * radius;
    					cperi = 2 * PI * radius;
    
    					printf("The area of the cirlce is %.2f.\n", carea);
    					printf("The circumference of the circle is %.2f.\n", cperi);
    					break;
    		case (4) :	printf("Please enter the base: ");
    					scanf("%d",&base);
    					printf("Pleare enter the left side: ");
    					scanf("%d", &lside);
    					printf("Please enter the right side: ");
    					scanf("%d",&rside);
    					printf("Please enter the height: ");
    					scanf("%d", &theight);
    
    					tperi = base + lside + rside;
    					tarea = (base * theight) / 2;
    
    					printf("The area of the triangle is %d.\n", tarea);
    					printf("The perimeter of the triangle is %d.\n", tperi);
    					break;
    		case (5) : EOF;
    			break;
    
    		default :	printf("Invalid choice entered.\n");
    					printf("Please enter a value between 1 and 5\n");
    					break;
    					}
    		}
    		while (chose != 5);
    
    }
    Last edited by clear; 04-15-2003 at 08:04 PM.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Originally posted by Speedy5
    1) Don't use void main(). Use int main().
    2) Use code tags (information on the top of every board).
    3) EOF; ?? What's this?
    4) Instead of that big if, it's better style to use a switch in this case.
    5) Look up scanf() in your documentation or on google. Here's a hint (correct me if I'm wrong, I rarely do C):
    Code:
    if (scanf("%d", num) != 1)
       printf("Invalid input!\n");
    About your scanf you're wrong, you forgot '&'.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    clear: Post with code tags please. You can use the "edit" button to amend your previous posts to make your code readable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>as for EOF it means end of file and exits the program.

    Not true, EOF is a Symbolic Integer Constant used to compare things to, not exit the program. If you wanted to do that you will want to use exit(0); with the stdlib.h header file. EOF will do nothing in this state. Its almost like putting -1; in the middle of your code. It just does nothing.

    --edit--

    What do you want the program to do if the user does enter some odd character instead of 1-5. Ask for another input like if you entered 7?
    Last edited by stumon; 04-15-2003 at 10:29 PM.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 10:45 AM
  2. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 08:31 AM
  3. Replies: 5
    Last Post: 01-13-2006, 12:00 AM
  4. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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