Computing.Net > Forums > Programming > c help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c help

Reply to Message Icon

Name: John
Date: January 30, 2002 at 19:13:59 Pacific
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Deb
Date: January 31, 2002 at 10:03:19 Pacific
Reply:

When you are printing the area using printf, don't use &area or &valx. Use &valx in scanf calls.


0

Response Number 2
Name: Deb
Date: January 31, 2002 at 10:06:18 Pacific
Reply:

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


0

Response Number 3
Name:  
Date: January 31, 2002 at 17:26:54 Pacific
Reply:

----------------


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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Driver programming Perl programmers in Vegas...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c help

Simple C Help!!! www.computing.net/answers/programming/simple-c-help/9795.html

C# help www.computing.net/answers/programming/c-help/3176.html

help help!!!..C...data file..please www.computing.net/answers/programming/help-helpcdata-fileplease/8092.html