LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-28-2004, 01:33 PM   #1
greg108
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: Reputation: 30
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,
 
Old 09-28-2004, 02:13 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
 
Old 09-28-2004, 02:45 PM   #3
greg108
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: Reputation: 30
thanks,

but also I was asking about double values that are not processed correctly.
What am I doing wrong?
 
Old 09-28-2004, 03:36 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 12:22 PM
string to double cynthia Programming 4 10-12-2004 02:40 AM
Why am I seeing double in X? linuxenthusiast Linux - Newbie 1 01-11-2004 02:23 PM
double conversion Xiangbuilder Programming 15 10-09-2003 09:12 AM
Double Up SpEcIeS Linux - General 1 08-26-2003 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration