Changings strings to variables

Started by
8 comments, last by cpp boy 20 years ago
Hi People ! I''m making a random number guessing game and I have a probleme I can only get my data in a form of strings. I know that changing variables to strings is a sinch but what about changing a string o a variable. If this isn''t possible well then here is my situation : I''m making a random number guessing game in window32 api and I''m using a edit box to get my data. I''m using the function : GetWindowText() to get the data out of the edit box and it comes in a string. Is there any way to get that in a form of number. Thanx in advance ! Kevin
Kevin
Advertisement
look up the functions

atoi()
atof()


Blaaaaa Blaaaa Blaa errrrr!!!! Bla?
basically atoi changes a given string buffer to an integer and i believe atof changes it to a real number (float variable). to get the integer just create an int variable to store the number in and a string variable to have for GetWindowText. for example
int number; //storage for the number.char string[5]; //4 numbers long + 1 terminating char.GetWindowText(/*bla bla*/);number = atoi (string);

also if u haven't already, u might want to make your edit box only accept numbers by adding ES_NUMBER to the style list (WS_CHILD etc.)

hope that helps

[edited by - the_moo on April 13, 2004 1:51:43 AM]
the_moo
Thanx guys. I love this community !

Kevin
Kevin
atoi is not standard C. It is not standard C++. It is not posix. It is windows dependent. For this situation, prefer boost::lexical_cast if you are using C++.

Edit: I realise the OP is using Win32. Even so, appropriate portability practices are never unwelcome.

[edited by - flangazor on April 13, 2004 10:29:28 AM]

Apologies for the confusion. I do believe I was thinking of itoa.

Thankyou for correcting me.

[edited by - flangazor on April 13, 2004 11:34:31 AM]
sscanf(string, "%d", &number);

Brianmiserere nostri Domine miserere nostri
search first: this question comes up more than anything

you may say you love this community but you're boring us to death by asking the same question again.

are you using c++? there are lots of nice safe ways to do this.

edit: link to other thread

[edited by - petewood on April 13, 2004 11:47:28 AM]
quote:Original post by flangazor
atoi is not standard C. It is not standard C++. It is not posix. It is windows dependent. For this situation, prefer boost::lexical_cast if you are using C++.

Edit: I realise the OP is using Win32. Even so, appropriate portability practices are never unwelcome.

<SPAN CLASS=editedby>[edited by - flangazor on April 13, 2004 10:29:28 AM]</SPAN>


So it''s not posix eh, well then I guess the man page is lying:

CONFORMING TO
SVID 3, POSIX.1, BSD 4.3, ISO/IEC 9899. ISO/IEC 9899:1990 (C89) and
POSIX.1 (1996 edition) include the functions atoi() and atol() only;
C99 adds the function atoll().
quote:Original post by flangazor
atoi is not standard C. It is not standard C++.


atoi() is a standard C99 and C++ function

quote:Original post by flangazor
atoi is not standard C. It is not standard C++. It is not posix. It is windows dependent.


Are you sure you don''t mean itoa?
atoi(), as previous posters have said, is standard.

-Mezz

This topic is closed to new replies.

Advertisement