-
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;
}
}
-
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");
-
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);
}
-
Quote:
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 '&'.
-
clear: Post with code tags please. You can use the "edit" button to amend your previous posts to make your code readable.
-
>>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?