The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Man command doesn't display any output amonst other problems djdavies AIX 6 02-02-2007 06:07 AM
Man command doesn't display any output amonst other problems :-) djdavies UNIX for Dummies Questions & Answers 14 02-01-2007 11:42 AM
How do I stop printf output from going into seperate txt files chrchcol Shell Programming and Scripting 12 07-26-2006 10:08 PM
find: problems escaping printf-command string grahamb Shell Programming and Scripting 1 12-04-2005 04:00 PM
fixed-width printf() output on an XmList on Solaris trido High Level Programming 0 11-20-2002 05:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-19-2008
pwanda pwanda is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 10
Exclamation Help please...output problems with printf.


Code:
#include <stdio.h>
#include <math.h>

// this function calculates the volume of a Cylinder
int main(void)
{
    int r;    // radius
    int h;    // height
    double M_PI; // pi
    
    int pOne = pow (r, 2);  
    
    // get user input of radius and height
    printf ("Enter your radius: ");
    scanf ("%f", &r);
    printf ("Enter your height: ");
    scanf ("%f", &h);

    // calculate volume
    
    volumeCylinder = pOne * M_PI * h;

    // output volume result
    
    printf ("The Volume of the Cylinder is: %lf\n", volumeCylinder);

    return 0;

}

Sample Output:

Code:
~Enter your radius: 3
~Enter your height: 2
~The Volume of the Cylinder is: 0.000000
~

The program compiles and runs but no matter what kind of printf format I do (double, float, int) I always get 0.0000..., -0.000..., or some really large number.

I don't know what could be wrong. Can anybody please help me with this? It is driving me insane.
  #2 (permalink)  
Old 10-19-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
1. You have calculate the power of r before assigning a value to it
2. The scanf function isn't used properly
3. M_PI is already defined in math.h
4. You haven't declare volumeCylinder before using it

Try this:


Code:
#include <stdio.h>
#include <math.h>

// this function calculates the volume of a Cylinder

int main(void)
{
    int r;    // radius
    int h;    // height
    double volumeCylinder; 

    // get user input of radius and height
    printf ("Enter your radius: ");
    scanf ("%d", &r);
    printf ("Enter your height: ");
    scanf ("%d", &h);

    // calculate volume

    int pOne = pow (r, 2);     

    volumeCylinder = pOne * M_PI * h;

    // output volume result
    
    printf ("The Volume of the Cylinder is: %lf\n", volumeCylinder);

    return 0;

}

Regards
  #3 (permalink)  
Old 10-19-2008
pwanda pwanda is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 10
Smile

Thanks for your help!

It didn't even occur to me to check placement. Oops.
  #4 (permalink)  
Old 10-19-2008
geekosaur geekosaur is offline
Registered User
  
 

Join Date: Oct 2008
Location: Pittsburgh, PA, USA
Posts: 20
You are using the %f format to scanf, but storing to an (int). (%f requires a (double) to receive the result. GNU C with -Wformat would warn you of this mistake.)
Closed Thread

Bookmarks

Tags
calculate, math.h, printf

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0