LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   double in C (https://www.linuxquestions.org/questions/programming-9/double-in-c-236269/)

greg108 09-28-2004 01:33 PM

double in C
 
I'm trying to write something simple in C and it doesn't work right.
Can someone help me with that code?

#include <stdio.h>


int main()
{
double num;
char option;

do
{
printf("Enter km/h: ");
scanf("%f", &num);
printf("%f\n", num);
printf("%f m/s", num * 0.277777778);
printf("\nDo you have another value (y/n)?");
scanf("%c%c", &option, &option); // scan twice -why?
} while ( option == 'y');

puts("\nDone");
return 0;
}

I get this strange output:

Enter km/h: 45
0.000000
0.000000 m/s
Do you have another value (y/n)?n

Done

Program exited normally.

Also when I scan for one character only the scanf() doesn't wait. I guess it read some \n character or something. Is there any way to fix it?

thanks,

itsme86 09-28-2004 02:13 PM

http://faq.cprogramming.com/cgi-bin/...&id=1043284392

greg108 09-28-2004 02:45 PM

thanks,

but also I was asking about double values that are not processed correctly.
What am I doing wrong?

itsme86 09-28-2004 03:36 PM

With scanf() you should use the %lf modifier instead of %f with doubles.


All times are GMT -5. The time now is 07:38 PM.