LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 02-17-2012, 10:21 AM   #1
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 36

Rep: Reputation: Disabled
Beginng C programming - how to input decimals from keyboard and calcuate as floats?


I've been modifying this basic conversion formula program to study basic data types. I can't seem to figure out how to properly input decimals (like 2.5) from the keyboard and use those values. Here's one version of the program:

Code:
#include<stdio.h>
#include <stdlib.h>
void fahr_celsius(float, float, float);

int main(int argc, char *argv[]) {
int i;
if (argc == 4){
	for (i=1;i<argc;i++)
		printf("Argument %d: %s\n", i, argv[i]);

puts("\nFarhenheit-Celsius conversion table\n");
fahr_celsius(atol(argv[1]), atol(argv[2]), atol(argv[3]));
}
else
puts("\nWrong number or type arguments!!\n");
return 0; 
}

void fahr_celsius(float min, float max, float step) {
float fahr;

printf("min = %3.2f max = %3.2f step =%3.2f\n", min, max, step);
/* for (fahr = min; fahr <= max; fahr +=step) 
	printf("%3.2f %8.2f\n", fahr, (5./9.) * (fahr - 32.0)); */
	putchar('\n');
}
Notice I've commented out the calculation until I figure out how to pass the values properly.. and there's no input checking yet, etc.

Here's what the output looks like:

Code:
$convrt 0.8 20.6 2.5

Argument 1: 0.8
Argument 2: 20.6
Argument 3: 2.5

Farhenheit-Celsius conversion table

min = 0.00 max = 20.00 step =2.00
There doesn't seem to be a ASCII-to-float conversion function available, or is there? What's the proper way to do this? Should the main prototype be
Code:
int main(argc, *float argv[])
instead, or some other simliar strategy?

Bonus question: when I get around to checking the input for valid arguments (no lettters or other non-numeric characters), should I check each character using getc or each string using string functions?

Thanks,
Keith Ostertag
 
Old 02-17-2012, 10:39 AM   #2
Guttorm
Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 959

Rep: Reputation: 157Reputation: 157
Hi

You're close. "atol" converts to long and "atof" converts to double. You can cast it to float like this:
Code:
float f = (float) atof(argv[1]);
Edit:
Also, if reading from files/strings/input you can use fscanf, sscanf or scanf. You can think of it as the "opposite" of printf. It has the advantage that it returns the number of successful conversions, while atof does not detect errors. It also has lots of other options. See man "scanf".

Code:
float f;
if (sscanf(argv[1], "%f", &f) != 1) {
  printf("Not a number");
}

Last edited by Guttorm; 02-17-2012 at 10:49 AM.
 
1 members found this post helpful.
Old 02-17-2012, 10:51 AM   #3
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 36

Original Poster
Rep: Reputation: Disabled
Thanks Guttorm- yes, that works. The call to the function now reads:
Code:
fahr_celsius((float) atof(argv[1]), (float) atof(argv[2]), (float) atof(argv[3]));
and it produces the expected output. I'll have to ponder that a bit...

Is that the best way to approach it? Seems a bit crude... I wonder if there is a more direct or simpler way? I'm a rank beginner, so I wouldn't know...

Keith

EDIT: Actually it appears the cast isn't necessary?

Code:
fahr_celsius(atof(argv[1]), atof(argv[2]), atof(argv[3]));
works as well.

Last edited by keithostertag; 02-17-2012 at 11:02 AM.
 
Old 02-17-2012, 11:07 AM   #4
Guttorm
Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 959

Rep: Reputation: 157Reputation: 157
Hi

I think the float casting is not really necessary, but the compiler can make a warning because you lose precision when converting from double to float. Maybe simpler to just use double instead of float? Then you get more precision as well. You can use sscanf if you want to check for invalid input.
 
Old 02-17-2012, 11:11 AM   #5
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 36

Original Poster
Rep: Reputation: Disabled
OK, thanks. I'll play with it some more. Marked as solved.

Keith
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read keyboard status (not: wait for keyboard input). stf92 Programming 14 07-03-2011 05:55 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
Need to calcuate modelines -- videogen Penguin of Wonder Linux - Software 3 02-17-2006 12:23 AM
How can I calcuate disk space from cylinders, heads, and sectors? abefroman Linux - Hardware 2 08-14-2005 09:59 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration