trouble with my first c program

This is a discussion on trouble with my first c program within the C Programming forums, part of the General Programming Boards category; Hi, this is my first post on these forums, I searched through the faq and couldnt find an answer so ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    trouble with my first c program

    Hi, this is my first post on these forums, I searched through the faq and couldnt find an answer so I am asking here. Im not even sure if its possible, but I am trying to make it so that my program won't round the result when its printed to only two decimal places on the screen. Thanks in advance for any help.

    Code:
    /* Programmer: Michael Blazar
       Objective:To find out the cost of wallpapering a spherical room with a diameter of 6084 and the cost of the wallpaper input by the user */
    #include <stdio.h>
    #define PI  3.14159265
    
    int main(void)
    {
       float cost; 
        int DIAMETER = 6084;
        
       printf("What is the cost of wallpaper per square meter in dollars?\n");
        scanf("%f", &cost);
        printf("The spherical room costs $%.2f to wallpaper.", DIAMETER * cost * PI);
        return 0;
    }
    Last edited by mblazar; 01-27-2005 at 09:26 PM.

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Please us code tags when you post code. Thanks.
    This probably isn't the best answer, but if you don't want it to round, then why don't you just subtract .005 from the value. That way if it would have rounded, now it won't.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    Thanks, I guess that works, Im sorry for not using the code tags. Im just wondering if there isnt another way of going about this?

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You could try my snip function!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    // Snip the double value of where you want it to be snipped
    double
    snip (double d,   // The decimal value we want to snip
          int dp)     // The decimal places of where to snip
    {
      // Bring the decimal number up to an integer of where we want to cut
      // And snip it
    
      d = floor (d * pow (10, dp));
    
      // Bring the integer back to a decimal by moving the decimal place back to the left
    
      d = d / pow (10, dp);
    
      // Successfully return the snipped version of the decimal
    
      return d;
    }
    
    int main (void)
    {
      // n = The number we want to snip
      // n2 = The result of the snipping
    
      double n = 3.14823723;
      double n2;
    
      // Snip the large decimal
    
      n2 = snip (n, 2);
    
      // Print out the results
    
      printf ("%f = ~%f\n", n, n2);
    
      // Return successfully
    
      return 0;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    I don't get it - it outputs just two decimal places here just fine
    Code:
    What is the cost of wallpaper per square meter in dollars?
    1
    The spherical room costs $19113.45 to wallpaper.
    What answer did you get?
    What answer did you expect?

    I'm assuming this is a test, because that isn't the formula for the surface area of a sphere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I am trying to make it so that my program won't round the result when its printed to only two decimal places on the screen.
    Code:
    printf("The spherical room costs $%.2f to wallpaper.", DIAMETER * cost * PI);
    If you don't want it to round to 2 decimal places, then don't include the .2 specifier in the %f string manipulator. Change %.2f to %f.
    Last edited by Scribbler; 01-28-2005 at 12:45 AM.

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Silly boy.

    The surface area of a sphere is 4(pi*r≤).

    If this were measured in m≤, then you'd just multiply whatever that formula gave you, times the cost.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a bit of trouble with a binary search program
    By d-dub in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2006, 05:20 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21