Advertisement

how to convert a char* to a char or int??

Started by January 05, 2003 12:59 PM
5 comments, last by baddrizz 21 years, 10 months ago
hey well i''m trying to make one of those random number games for a friend now i never really understood pointer''s so thats prob why i''m having this prob. i''m using command line arguments to set how high the the number range is like rand.exe 25 would make the number range from 1 to 25 i tried using ( rand() % argv[1]) + 1 but that gives some number that way to high if i used 25 so i''v tried converting it to and int and char to use with rand() bu so far none of these have worked what am i doing wrong? int number = (int)argv; this one gives 4722304 char number = (char)argv; and this one gives a crazy C character int number = argv[1]; gives another crazy number char number = argv[1]; give another symoble i''v tried a couple other way i could think of but so far nothing has worked any idea''s on how to get argv to work with rand()? thx for any help
Hey There,

If I remember correctly...all command line arguments are handled as strings in C++. So, try using the "atoi()" function which should convert from a char value to an integer. Take a look on the MSDN to find out how to use it.

Mike
Mikehttp://cs.wpunj.edu/~bowersom
Advertisement
Sorry, I can't reproduce your problem right now, but the above poster is correct. Here's an example of using atoi the way you'll want to:

char ParamBuffer[16];
atoi(argv[1], ParamBuffer, 10);

The last number is the radix, or base. Since you want your number in plain ol' decimal, it's 10.

the above example is wrong. see below

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on January 6, 2003 1:02:16 PM]

[twitter]warrenm[/twitter]

Hmm, use int number = atoi(argv[1]);

or if you want it obfuscated:

int main(int argc, char *argv[])
{printf("%d", (rand()%atoi(argv[1])+1));return 0;}

I have no clue what kind of atoi the guy above this post has, because I''ve never seen an atoi implementation that uses more than one parameter .
quote:
char ParamBuffer[16];
atoi(argv[1], ParamBuffer, 10);

This is actually supposed to beitoa (int to ascii). Zealous just misinterpreted the question.
-- SEE EDIT BELOW --

You sure tuita? AFAIK, command lines can be recieved as an array of char pointers, leading me to believe that the best way to get a number into a program by this method would be by converting a command-line param to a number, via atoi , but I could just be insane. Like I said, I didn't have time to test that solution.

EDIT Yeah, the function is atoi, but my example was bogus. Usage should just be
int Number = atoi(argv[x]);

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on January 6, 2003 1:01:28 PM]

[twitter]warrenm[/twitter]

Advertisement
I would like to point your attention to the possibility of using stringstreams as well. Not that they''re necessary in such a simple case, but you might find them inspirational when solving future problems.

I''m a little fuzzy-headed at the moment, it being 3 AM, but stringstream documentation and examples should be readily available.

Choose Freedom
Free Compiler and IDE:
[MinGW] [V IDE]
The Standard Template Library and Boost:
[boost] [STL] [STLport]
Free Media Libraries:
[SDL] [OpenGL] [OpenAL] [DevIL]

This topic is closed to new replies.

Advertisement