Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-28-2004, 01:33 PM
|
#1
|
Member
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170
Rep:
|
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,
|
|
|
09-28-2004, 02:13 PM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
|
|
|
09-28-2004, 02:45 PM
|
#3
|
Member
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170
Original Poster
Rep:
|
thanks,
but also I was asking about double values that are not processed correctly.
What am I doing wrong?
|
|
|
09-28-2004, 03:36 PM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
With scanf() you should use the %lf modifier instead of %f with doubles.
|
|
|
All times are GMT -5. The time now is 12:22 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|