Perhaps I'm making some newbie, bonehead mistake here, but I can't see what it is. I wrote a simple program which writes a table of celsius-to-fahrenheit conversions. I've included the entire program at the bottom of this post for reference, but the guts of the program are the following lines:
Why the extra blank lines? printf() doesn't automatically print a newline, does it? Don't you have to put those in manually? So I don't see why I'm getting extra blank lines. Am I missing something obvious?
Entire program, for reference:
/********************************************************************\ * Program name: FahrCelsTable * * Description: Fahrenheit-to-Celsius Table Generator * * File name: fahrcelstable.c * * Source for: fahrcelstable.exe * * Author: Robbie Hatley * * Date written: Sat May 01, 2004 * * Inputs: Three CLI args: begin, end, increment. * * Outputs: Sends chart to cout. Can be redir'ed to a file. * * To make: No dependencies. * * Edit history: * * Thu Nov 23, 2006: Split FahrCelsTable from FahrCels. * \********************************************************************/
void Help (void) { printf ( "FahrCelsTable must have 3 arguments:\n" " Fahrenheit Min\n" " Fahrenheit Max\n" " Fahrenheit Increment\n" "Max must be at least 0.001 greater than Min,\n" "and Increment must be at least 0.001\n" "FahrCelsTable will then print a table of Fahrenheit-to-Celsius conversions\n" "for the range and increment you specified." ); return;
}
-- Cheers, Robbie Hatley lonewolf at well dot com www dot well dot com slant tilde lonewolf slant
> Perhaps I'm making some newbie, bonehead mistake here, but > I can't see what it is. I wrote a simple program which > writes a table of celsius-to-fahrenheit conversions. > I've included the entire program at the bottom of this post > for reference, but the guts of the program are the following > lines:
> Why the extra blank lines? printf() doesn't automatically > print a newline, does it? Don't you have to put those in > manually? So I don't see why I'm getting extra blank lines. > Am I missing something obvious?
What happens when you leave out the "\n"?
What about just doing printf ("abc\ndef\n"); ?
Or puts("abc"); puts("def"); ?
(I'm assuming the display width is more than about 24 characters.) -- Bartc
printf() isn't automatically inserting newlines. I didn't think it would, as other programs I've written and compiled on same compiler don't put them in.
puts() *is* inserting newlines, but then, it's supposed to.
So none of the above explains the extra newlines in my program.
-- Cheers, Robbie Hatley lonewolf at well dot com www dot well dot com slant tilde lonewolf slant
<see.my.signat...@for.my.contact.info> wrote: > Perhaps I'm making some newbie, bonehead mistake here, but > I can't see what it is. I wrote a simple program which > writes a table of celsius-to-fahrenheit conversions. > I've included the entire program at the bottom of this post > for reference, but the guts of the program are the following > lines:
> Why the extra blank lines? printf() doesn't automatically > print a newline, does it? Don't you have to put those in > manually? So I don't see why I'm getting extra blank lines. > Am I missing something obvious?
> Entire program, for reference:
> /********************************************************************\ > * Program name: FahrCelsTable * > * Description: Fahrenheit-to-Celsius Table Generator * > * File name: fahrcelstable.c * > * Source for: fahrcelstable.exe * > * Author: Robbie Hatley * > * Date written: Sat May 01, 2004 * > * Inputs: Three CLI args: begin, end, increment. * > * Outputs: Sends chart to cout. Can be redir'ed to a file. * > * To make: No dependencies. * > * Edit history: * > * Thu Nov 23, 2006: Split FahrCelsTable from FahrCels. * > \********************************************************************/
> void Help (void) > { > printf > ( > "FahrCelsTable must have 3 arguments:\n" > " Fahrenheit Min\n" > " Fahrenheit Max\n" > " Fahrenheit Increment\n" > "Max must be at least 0.001 greater than Min,\n" > "and Increment must be at least 0.001\n" > "FahrCelsTable will then print a table of Fahrenheit-to-Celsius conversions\n" > "for the range and increment you specified." > ); > return;
> }
> -- > Cheers, > Robbie Hatley > lonewolf at well dot com > www dot well dot com slant tilde lonewolf slant
I inadvertantly included the source for my "Fahrenheit-to-Celsius" program, not the "Celsius-to-Fahrenheit" one. Now I see why the two programs behave differently! Too many files open in NoteTab; I grabbed the wrong one. Sorry about the false alarm.
As is obvious from the source, the problem is an extra \n :
> Perhaps I'm making some newbie, bonehead mistake here, but > I can't see what it is.[...]
Here's something I noticed:
> but what it *actually* prints is:
> Cels Fahr > 40.000 104.000
> 40.200 104.360
... contrasted with the source line
> printf(" Fahr Cels\n");
Note that the column headings are desrever. Also, note that while 40 Celsius is 104 Fahrenheit, 40 Fahrenheit is 4.44 Celsius, not 104: The direction of conversion is also desrever. The output is not from some other program, not from the source code you showed.
> printf() isn't automatically inserting newlines. > I didn't think it would, as other programs I've written > and compiled on same compiler don't put them in.
OK, so printf() works as it should. So now, starting either with a simple, working printf and adding to it, or the other way, try and see where the problem creeps in.
These are suggestions (or perhaps put all of them in the loop; after which one is there an extra newline?):
Then that might give a clue as to what's causing the problem (as I can't recreate it). Maybe a hidden control character in the format string (I didn't notice one)?
> [...] > Note that the column headings are desrever. Also, note > that while 40 Celsius is 104 Fahrenheit, 40 Fahrenheit is > 4.44 Celsius, not 104: The direction of conversion is > also desrever. The output is not from some other program, > not from the source code you showed.
Too much editing, not enough proofreading: That final sentence should have been "The output is from some other program, not from the source code you showed."