I believe you mean you have the string 15, as in "15"? And you want to convert that into an int, right? Then you can use strtod.
Make sure you direct some file to it in the command line. If not it will enter in a cat mouse chaseCode:#include <stdio.h> int main( void ) { char buf[BUFSIZ]; float var[255]; /* big enough to hold 255 entries */ int i = 0; /* * read from standard input stream line by line * and format any float */ while( fgets( buf, sizeof buf, stdin ) ) { if( sscanf( buf, "%f", &var[i] ) > 0 ) { /* display */ printf( "%.2f\n", var[i] ); ++i; } } return 0; }![]()
Last edited by Aia; 02-01-2008 at 05:19 PM.
this code is actually right:
however instead of printing, I would like to store argv[i] into a char, say argv[i] is '15.0' and I want a double value of 15.0Code:int main(int argc, char* argv[]) { int i; for(i=0; i<argc; i++) printf("%s\n", argv[i]); getchar(); }
Last edited by -EquinoX-; 02-01-2008 at 05:48 PM.
OK, here's how it's done:
If you want a string, for example "15.0", to be converted into an integral type, then you use strtod for string to double.
If you just want the representational number that represents a character, then you use a cast.
For example, the value which represents the character '1' in multi-byte on Windows, which is 49, then do a cast.
okay hold on, before preceeding to my next confusion, can I do this:
double temp = (double)argv[1][1];
so when I type in ./donut 3.0
temp will have a value of 3.0 as a double not as a char
No. First it should be argv[1], because it's a pointer to strings. And a string is an array of char.
What you get, is 52. What the heck, you say? Why 52? Because it's the value used to represent the character 3 on multi-byte on Windows.
Use strtod to properly parse the string and return a double representation of that string. Or in other words, to return 3.0.
elysia, now I am able to get what you said 2 posts ago.
when I typed in 1 the out put is 49. You mentioned to do a cast. So I cast it to a double right?? by just adding the (double) in front?
here's my code:
when I typed in ./donut 1.0Code:#include <stdio.h> int main(int argc, char *argv[]) { double dozen_donut_cost; int donuts_eaten; int i; double donuts_cost; double temp = (double) argv[1][0]; printf("%g\n", temp); return 0; }
the result is
49
it gives me this error implicit declaration of function strtod and it doesn't give me 1.0 when I execute the program using ./donut 1.0
Code:#include <stdio.h> int main(int argc, char *argv[]) { double dozen_donut_cost; int donuts_eaten; int i; double donuts_cost; double temp = strtod(argv[1], NULL); printf("%g\n", temp); return 0; }
Last edited by -EquinoX-; 02-01-2008 at 06:55 PM.
Implicit declaration of function means you haven't created a prototype for your own functions or in the case of library functions, you haven't included the correct header. So typically you would look into some documentations to find the correct header. For strtod, it's stdlib.h.
And no, it should be argv[1]. Remember that argv is a array of strings. Strings themselves are an array of char. So it becomes string* or string[], but since string is char[], it becomes char[][].
You must pass a string, not a character.
thanks Elysia it works, I had one question though an important one:
say that I want to do ./donut< t0,
where t0 is file that contains 3.0 1.0 2.0.... (all in one line)
how do I get the 3.0 value 1.0 value, etc??
my instructor says that I should use scanf to get the t0? but how do I do this?
what if I do this at the beginning of the program:
will it work if I do ./donut < t0?Code:#include <stdio.h> int main () { char temp; scanf("%s", &temp); printf("%s\n", temp); return 0; }
Last edited by -EquinoX-; 02-01-2008 at 08:06 PM.
Q: What's temp?
A: A tiny, tiny, one byte of holding storage in most systems.
Q: What are you telling scanf to do with %s?
A: Hey, get this string ( an array of chars with '\0' that is invisible behind every string ) and
push it into this tiny one byte variable named temp.
Q: Do you think that is going to work?
A: Nope, but ...I though...I am just learning...ah,...maybe...it will work for me just this one time?
Code:#include <stdio.h> int main( void ) { char buf[BUFSIZ]; float var[255]; /* big enough to hold most entry? */ int i = 0; int x = 0; /* read the standard input stream */ while( scanf( "%f", &var[i] ) > 0 ) ++i; /* testing outside the while loop */ for( x = 0; x < i; x++ ) { printf( "var[%d] = %.2f\n", x, var[x] ); } return 0; }
how do I resolve this problem??
Code:warning: format â%lfâ expects type âdouble *â, but argument 2 has type âdoubleâ