How do I calculate within a printf??

Discussion in 'Mac Programming' started by cybrscot, Dec 25, 2010.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    My text wants me to calculate (volume + 165) / 166 within the last printf

    I'm not including the entire program, but you will understand, I'm only showing you the last printf line.

    The first thing I did was

    printf (" Dimensional Weight (pounds): (volume + 165) / 166:\n") ;

    output was : Dimensional Weight (pounds): (volume + 165) / 166
    /*not what I was looking for*/


    The second thing I did was

    printf (" Dimensional Weight (pounds): (%d + 165) / 166:\n" , volume) ;

    output was: Dimensional Weight (pounds): (960 + 165)/ 166
    /* although the placeholder was filled in with the volume, now I just have the value of the volume, rather than the word volume, still not calculated, so not what I was looking for*/

    How do I get output: Dimensional Weight (pounds): 6

    I know I can have an initializer such as int weight = (volume + 165) / 166 ;

    and use a placeholder for weight and get the calculation, but my book specifically asked that I (and I quote the book) "remove the weight variable, instead calculating (volume + 165) / 166 within the last printf

    I'm learning this on my own from a book, I've come a long, long way in 2 days. No schools to teach C here where I'm currently living. So I give much thanks for your help, and hope you will be gracious enough to continue helping me in the future.
    Thanks again, and Merry Christmas.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Use %d as the format specifier, then do the calculation outside the format string as the second argument.

    -Lee
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    Thanks Lee1210

    can you write the example code? I'm not sure I follow you on how to do that? BTW, we haven't discussed arguments yet, it comes much later in the book.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I can, but it will be better if you do it yourself.

    You are already using arguments even if you don't know it. After the format string in one of your examples you have a comma then your variable "volume". Instead of a variable, put in the expression with your calculation.

    -Lee
     
  5. cybrscot, Dec 25, 2010
    Last edited: Dec 25, 2010

    cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    okay, thanks, I'll try that. Merry Christmas

    hahaha! I did it!!

    my code:
    printf (" Dimensional Weight (pounds): %d\n", (volume + 165) / 166) ;

    It worked!
    output: Dimensional Weight (pounds): 6

    You da man!!!
     
  6. BertyBoy macrumors 6502

    Joined:
    Feb 1, 2009
    #6
    Getting there.

    Code:
    printf (" Dimensional Weight (pounds): %d\n" , ( volume + 165) / 166 );
    
    Best way to learn this early is to look for sample code, there's plenty out there.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    Great. Better than me just doing it, eh?

    -Lee
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    Just a thought.

    It may be tempting to do this, combine the calculation and output functions, but depending on your goals for learning programming you may want to try and resist that from the beginning.

    Imagine down the line, that you want to make this in to a GUI program and output the Dimensional Weight in a different way. If you keep the calculation and display functions separate (or at least easily separable) your code becomes easier to read and more importantly modify and reuse.

    B
     

Share This Page