Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
im new with c and im taking a class and need this done by tomorrow, can sombody tell me whats wrong?
#include
main ()
{
float area;
int valx, valy;
char shape;
printf("This program will calculate the area of a shape\n");
printf("Press C for circle, S for square, R for rectangle and T for triangle:");
scanf("%c" ,&shape);
fflush(stdin);
shape=toupper(shape);
if(shape=='C')
{
printf("Enter the Radius of the circle:");
scanf("%d",&valx);
area = valx*3.14;
printf("The area of a circle with a radius of is:%9.2f\n",&area);
}
else
if(shape=='S')
{
printf("Enter the length of a side of the square:");
scanf("%d",&valx);
area = valx*valx;
printf("The area of a square when one side has a length of %f is %9.2f\n", &valx, &area);
}
else
if(shape=='R')
{
printf("Enter the length and height of the rectangle:");
scanf("%d%d",&valx,valy);
area = valx*valy;
printf("The area of the rectangle with a length of %f and width of %f is %9.f\n",&valx,&valy,&area);
}
else
if(shape=='T')
{
printf("Enter the base and heigth of the triangle:");
scanf("%d%d"); valx,valy;
area=valx*valy/2;
printf("The area of a triangle with %f as the base and %f as the height is %9.f\n",&valx,&valy,&area);
}
else
printf("Wrong letter!\n");
return(0);
}

missed in my earlier response, the printf statements should look like :
printf("The area of a square when one side has a length of %f is %9.2f\n", valx, area);

----------------
//area = valx*3.14;
area = 3.14 * valx * valx;
// or, with math.h, you can use: area = 3.14 * pow(valx, 2);
// printf("The area of a circle with a radius of is:%9.2f\n",&area);
// missed one specifier %d
// &area will print out the address in memory where 'area' is stored
// instead of your desired variable 'area'
printf("The area of a circle with a radius of %d is: %.2f", valx, area);
// printf("The area of a square when one side has a length of %f is %9.2f\n", &valx, &area);
// use %d specifier because valx is declared as int
printf("The area of a square when one side has a length of %d is %.2\n", valx, area);
// if you want to view valx as floating point notation:
printf("The area of a square when one side has a length of %f is %.2f\n", (float)valx, area);
// this seems pointless however, since your entering intergers
//scanf("%d%d",&valx,valy);
scanf("%d %d", &valx, &valy);
// you need to pass the address of the variable 'valy'
// when using scanf()
//printf("The area of the rectangle with a length of %f and width of %f is %9.f\n",&valx,&valy,&area);
printf("The area of the rectangle with a length of %d and width of %d is %f\n", valx, valy, area);
//printf("The area of a triangle with %f as the base and %f as the height is %9.f\n",&valx,&valy,&area);
printf("The area of a triangle with %d as the base and %d as the height is %f\n", valx, valy, area);

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |