Some newbie casting issues

Discussion in 'C Programming' started by SabaUd, Dec 1, 2007.

  1. SabaUd

    SabaUd Guest

    Hello,
    I wrote a short program that needs to calculate capacity of stack.
    i have 2 ints and im trying to do so.
    my code:

    Stack* selectedStack = ...;
    double fActual = (double)selectedStack->nActualSize;
    double fAllocated = (double)selectedStack->nAllocatedSize;
    double fCapacity = (fActual / fAllocated) * 100.0;
    printf("%d ", (int)ceil(fCapacity));

    Now - this one works, but it's disgusting code...

    I'm sure there's a more elegant way to execute it...

    any ideas?
     
    SabaUd, Dec 1, 2007
    #1
    1. Advertising

  2. SabaUd

    SabaUd Guest

    oh, another one I forgot:
    I want to print "30%"...
    how do I print '%'?
     
    SabaUd, Dec 1, 2007
    #2
    1. Advertising

  3. SabaUd

    Simon Gerber Guest

    SabaUd @ Sat, 01 Dec 2007 03:52:54 -0800:

    > how do I print '%'?


    by typing %%

    --
    Simon Gerber
    simugerber (at) student (dot) ethz (dot) ch
     
    Simon Gerber, Dec 1, 2007
    #3
  4. SabaUd said:

    > oh, another one I forgot:
    > I want to print "30%"...
    > how do I print '%'?


    See page 13 of "The C Programming Language", 2nd edition, by Kernighan and
    Ritchie.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 1, 2007
    #4
  5. SabaUd

    santosh Guest

    SabaUd wrote:

    > Hello,
    > I wrote a short program that needs to calculate capacity of stack.
    > i have 2 ints and im trying to do so.
    > my code:
    >
    > Stack* selectedStack = ...;
    > double fActual = (double)selectedStack->nActualSize;
    > double fAllocated = (double)selectedStack->nAllocatedSize;
    > double fCapacity = (fActual / fAllocated) * 100.0;
    > printf("%d ", (int)ceil(fCapacity));
    >
    > Now - this one works, but it's disgusting code...
    >
    > I'm sure there's a more elegant way to execute it...
    >
    > any ideas?


    Yes. Provide separate functions that take a Stack * argument and return
    details (like capacity, size etc.) about it. And consider using size_t
    as the object type for returning size and capacity related values,
    since that is the natural type for such purposes in Standard C.
     
    santosh, Dec 1, 2007
    #5
  6. SabaUd <> writes:

    >Hello,
    >I wrote a short program that needs to calculate capacity of stack.
    >i have 2 ints and im trying to do so.
    >my code:
    >
    >Stack* selectedStack = ...;
    >double fActual = (double)selectedStack->nActualSize;
    >double fAllocated = (double)selectedStack->nAllocatedSize;


    These two casts aren't actually needed, unless you're using a very strict
    compiler or you're compiling this as C++ code.

    >double fCapacity = (fActual / fAllocated) * 100.0;
    >printf("%d ", (int)ceil(fCapacity));


    This cast, however, is needed so that printf() is passed the right type of
    value.

    --
    You can't write me, I :q!
     
    Minimiscience, Dec 2, 2007
    #6
  7. SabaUd <> wrote:
    > I wrote a short program that needs to calculate capacity
    > of stack.
    > i have 2 ints and im trying to do so.
    > my code:
    >
    > Stack* selectedStack = ...;
    > double fActual = (double)selectedStack->nActualSize;
    > double fAllocated = (double)selectedStack->nAllocatedSize;
    > double fCapacity = (fActual / fAllocated) * 100.0;
    > printf("%d ", (int)ceil(fCapacity));
    >
    > Now - this one works, but it's disgusting code...


    Assuming nXXXXX are indeed integers...

    printf("%3.0f%%\n", 100.0 * selectedStack->nActualSize
    / selectedStack->nAllocatedSize);

    --
    Peter
     
    Peter Nilsson, Dec 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,840
    Andrew McDonagh
    Jan 8, 2005
  2. Jess

    some casting questions

    Jess, Jun 16, 2007, in forum: C++
    Replies:
    9
    Views:
    372
    James Kanze
    Jun 17, 2007
  3. c++ casting issues

    , Mar 19, 2008, in forum: C++
    Replies:
    4
    Views:
    457
    James Kanze
    Mar 20, 2008
  4. Wally Barnes
    Replies:
    3
    Views:
    545
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    595
    James Kanze
    Dec 20, 2009
Loading...

Share This Page