printf(" Fahr Cels\n");
for (Fahr = FahrMin; Fahr <= FahrMax; Fahr += FahrInc)
{
Cels = (((Fahr-32)*100.0)/180.0);
printf("%10.3f %10.3f\n", Fahr, Cels);
}
When I run "CelsFahr 40 41 0.2, I expected it to print:
Cels Fahr
40.000 104.000
40.200 104.360
40.400 104.720
40.600 105.080
40.800 105.440
but what it *actually* prints is:
Cels Fahr
40.000 104.000
40.200 104.360
40.400 104.720
40.600 105.080
40.800 105.440
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. *
\********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void Help (void);
int main(int Fred, char* Ethel[])
{
double FahrMin=0, FahrMax=0, FahrInc=0, Fahr=0, Cels=0;
if ( 2 == Fred && ( 0 == strcmp(Ethel[1], "-h") || 0 == strcmp(Ethel[1], "--help") ) )
{
Help();
return 777;
}
if (4 != Fred)
{
printf("FahrCelsTable must have 3 args.:\n");
printf(" Fahrenheit Min\n");
printf(" Fahrenheit Max\n");
printf(" Fahrenheit Increment\n");
return 666;
}
FahrMin = atof(Ethel[1]);
FahrMax = atof(Ethel[2]);
FahrInc = atof(Ethel[3]);
if (FahrMin > FahrMax - 0.001)
{
printf
(
"Error: maximum must be at least 0.001 greater than minimum.\n"
);
return 666;
}
if (FahrInc < 0.001)
{
printf
(
"Error: increment must be at least 0.001\n"
);
return 666;
}
printf(" Fahr Cels\n");
for (Fahr = FahrMin; Fahr <= FahrMax; Fahr += FahrInc)
{
Cels = (((Fahr-32)*100.0)/180.0);
printf("%10.3f %10.3f\n", Fahr, Cels);
}
return 0;
}
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
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
> 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.)
Ok, I'll test those. Here's a simple program:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
printf("abcd");
printf("efgh");
printf("ijkl");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("abc\ndef\n");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
puts("abc"); puts("def");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
return 0;
}
Compiled with same compiler (gcc).
OUTPUT:
abcdefghijkl
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
abc
def
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
abc
def
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
Are you sure that this is really the line of code, and not
printf("%10.3f %10.3f\n\n", Fahr, Cels);
If you have one '\n' newline character, and you have a standard 80
char line output, there must be a typo somewhere.
As is obvious from the source, the problem is an extra \n :
"Celsius-to-Fahrenheit":
printf(" Cels Fahr \n");
for (Cels = CelsMin; Cels <= CelsMax; Cels += CelsInc)
{
Fahr = (((Cels*180.0)/100.0)+32);
printf("%10.3f %10.3f\n\n", Cels, Fahr); /* Extra \n */
}
"Fahrenheit-to-Celsius":
printf(" Fahr Cels\n");
for (Fahr = FahrMin; Fahr <= FahrMax; Fahr += FahrInc)
{
Cels = (((Fahr-32)*100.0)/180.0);
printf("%10.3f %10.3f\n", Fahr, Cels); /* NO extra \n */
}
DOH!!!
--
Oops!
> ... must be a typo somewhere ...
Close. I copy-n-pasted source code for wrong program altogether.
Sorry about the confusion. (See my post "Oops, wrong program source!").
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.
--
Eric Sosman
eso...@ieee-dot-org.invalid
> Ok, I'll test those. Here's a simple program:
> printf("abcd");
> printf("efgh");
> printf("ijkl");
...
> OUTPUT:
>
> abcdefghijkl
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> abc
> def
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> abc
> def
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> 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?):
printf("<>\n");
printf("<%d>\n", (int)Fahr);
printf("<%10d>\n", (int)Fahr);
printf("<%10.3f>\n", Fahr);
printf("<%10.3f %10.3f>\n", Fahr, Cels);
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)?
--
Bartc
Too much editing, not enough proofreading: That final
sentence should have been "The output is from some other