Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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.
 
Use %d as the format specifier, then do the calculation outside the format string as the second argument.

-Lee
 
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.

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
 
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!!!
 
Last edited:
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.
 
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!!!

Great. Better than me just doing it, eh?

-Lee
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.