# 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. ## 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 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("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){
scanf("%d", &rwidth);
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){
scanf("%d", &swidth);
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){

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){

scanf("%d",&base);
printf("Pleare enter the left side\n");
scanf("%d", &lside);
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. 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. ## 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;
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("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);
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;

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);
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);

}```

4. 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");```