Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tImmaY
hey, i have to create a program that calculates the volume of a cylinder *please dont stop reading, i did the work and i dont want someone to do it for me - i need help* so i wrote the program and and i'm using floating point variables as is required.. but for some reason when the program does the calculation, it comes up with some huge, oddball number thats nowhere NEAR accurate. when i give it 10 for the height and 5 for the radius, with the .exe from MSVC++ i get 603979776 and in Dev-C++ i get 0. so what did i do wrong?? thx!
Source Code :
/* Cylinder.c by Tim Hansen 01/26/04 *//* This program will be used to calculate
* the volume of a cylinder. The radius
* and height variables will be input by
* the user via the keyboard. Output will
* be displayed on the screen.
*/#include <stdio.h>
int main()
{
/* variable declaration */
float height, radius, volume;/* prompt and input section */
printf("Cylinder Volume Calculation\n\n\n");printf("Enter the height ");
scanf("%d", &height);printf("\n\nEnter the radius ");
scanf("%d", &radius);
/* for space between the last input statement and the output */
printf("\n\n\n");/* calculation and storage of the cylinders volume */
volume = 3.14159f * radius * radius * height;/* output cylinder volume */
printf("The cylinder's volume is %d\n", volume);
return 0;
}

Hi
The only thing that I can see is thisscanf("%d", &height);
scanf("%d", &radius);I think it should be as you are input a float and not an integer.
scanf("%f", &height);
scanf("%f", &radius);

I'm not so familiar with C but I think that there is a mistake in "scanf" and "printf".
As I see you want to enter floating point variable. That's why you have to typescanf("%f", &height);
instead
scanf("%d", &height);
The same thing is for "printf".
And you have to enter numbers in the folowing format:
10.0It's good to initialize the variables before
using them.
i.e.float height=0.0, radius=0.0, volume=0.0;
I think that this will work.

hahahaha ^_^ *scratches head* ..oops..
it appears that in my haste, i made and overlooked a mistake. thanks guys.. lol

I for one just want to say "thanks" for doing your own work before coming here. It's a nice change from the norm.
Good job :)

![]() |
FoxPro DOS Command help
|
AddNew Problem in VB
|

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