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