Monday, May 24, 2010

My program in C will not read past the decimal point for a double value?

Alright, here's the input file, basically.


2 200707211425 F70.5.


I read everything into a character array, except for after the F, I use a double. Problem is, when I print out the double, I only get 70, as opposed to 70.5. Take a look at the code, if you will.





int i;


for(i=0; i %26lt; SIZE; i++)


{








int z;


for (z = 0; z %26lt; 12; z++)


{





fscanf(sourceFile, "%c", %26amp;date[z]);





}





printDate(date);


fscanf(sourceFile, "%c", %26amp;discard);


fscanf(sourceFile, "%c1", %26amp;type[i]);


printf("%c", type[i]);


fscanf(sourceFile, "%d", %26amp;temp[i]);


printf("%d", temp[i]);





my temp value for the print (which is a double) only displays 70, when I need it to get 70.5.





Also, I have a loop to run a function that if type[i]=='F' (which it does), to do the function, and that isn't working either. Any ideas?

My program in C will not read past the decimal point for a double value?
If you are expecting the line





fscanf(sourceFile, "%d", %26amp;temp[i]);


printf("%d", temp[i]);





to read and print the F70.5 line, then you should change %d to %f. Please see http://www.cplusplus.com/reference/clibr...
Reply:%d is an integer format. use %f to read/print the float or %lf for the double precision float


No comments:

Post a Comment