Advertisement

Using numbers in strings

Started by June 15, 2001 07:09 AM
7 comments, last by Biberpelztrompete 23 years, 8 months ago
Hi, how do I use numbers in strings ?? So how do I get the int casted into a LPCTSTR ?? I know the question is silly but I don''t know the answer though. MessageBox(NULL, value ???,"Error Message No.",MB_OK);
sprintf(string,"%i",integer);
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
Advertisement
As far as I know (and I am doing this off the top of my head) there are at least 3 ways to accomplish this. The first (and easiest, in my opinion) is to use the .Format() member function of a CString (I am assuming you are using MFC). Just do
  MyString.Format("%d", ErrorCode); // simple yet effective// you could also do something a bit more interesting with the same ideaMyString.Format("Error Code: %d", ErrorCode); // this gives a description instead of just the value.// then call MessageBox()MessageBox(NULL, MyString, "Error Message No.", MB_OK);   


You also have the option of a function called itoa(), which takes an int and turns it into a char * (I am not sure of the prototype, look it up in MSDN).

The third option, which has already been suggested, is using sprintf() which works exactly like CString::Format().

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Hi,
no I''m not using the MFC, but I think the sprintf method works. But I''m not sure about how to declare a string. There are

char xy[length]
LPSTR
LPCSTR
LPCTSTR ...

thanx anyway for your help




Best not to use MFC specific stuff where it can be avoided. Isn''t the MyString.format() method converting from string to int - the opposite of what the OP requested (it also assumes that an object called MyString has been constructed and initialized with a value)? As for itoa() - that is not a standard C++ function and should be avoided. Maximus''s solution is the one to use!
AFAIK, the string types you mentioned are all "long-pointer to string", which in windows these days is the same as just pointer to string. However, there are several types of string involved in C++ programming. The best type to use in general is std::string which is specified by the ISO C++ standard.

However, for your particular problem, LPSTR is equivalent to "char *", so you should be able to define your string as:

const int string_length = sizeof( int ) + 1 ;
char my_string[ string_length ] ;

The char[] array will automatically get passed as a pointer to char when calling sprintf.

Hope this makes sense.
Advertisement
Yep, it does. Thank you very much
Ok ... to get ANY data type into an old C style string (char array) you use sprintf() .. which has all the same rules and abilities as printf() ... BUT ... John''s post made a big mistake. You must have declare large enough string, and he does not.

His post says the string size should be sizeof(int) + 1 ... which would be 5 ...which is not what you want ... what you want in a string big enough to hold the string version of your int .. not the int itself ... so think for a second ... a 32 bit int can be as big as 4 billion and something ... which is 4,000,000,000 and might have a negative sign, plust the NULL character ... so with no decimals, no text, and no extra padding, you should use at least a 12 character array ... traditionally i used to use a 33 character array, which is the largest the old itoa function would ever expect (a 32 bit binary number plus NULL). Now ... I just allocate some buffer that''s 10-20 characters longer than I think I need ... when it''s in a function ... when it''s in a data structure I figure it out correctly.

the different string types exist for this reason ... LPSTR is a string of chars .. LPCSTR is a constant string of chars ... and the other ones with Ws and Ts exist for wide character strings and unicode (maybe).. In general you will always use LPSTR and LPCSTR ... or their equivelents which is just "char *" and "const char *".

IF you use the std C++ string class .. then you use the function c_str() to get a char* to the string''s buffer ...

so IF you already have a std::string called valString ... your message box would be called with:

  MessageBox(NULL,valString.c_str(),"Error Message No.",MB_OK);  


and if you want to get the value into a char string from scratch it would be

  char valStr[80];sprintf(valstr,"Error Number: %d",value);MessageBox(NULL,valStr,"Unexpected Error",MB_OK);  


Also ... please realize that these MessageBox''s are GREAT for debuging, but for the ones that stay in your production code you need much better messages than Error ... IF they are going to stay in your released code I recommend you include a lookup table of error messages to strings ... so you get a message bos like this:

TITLE: MyProgramName - Unexpected Error
MESSAGE: An unexpected error has occurred and the program will be shut down.
DETAILS: The video device returned error code: 323

etc etc ... good luck
Sorry, Xai''s absolutely right. I haven''t allocated enough space for the sprintf() result. Hard day at the office! The point I was trying to make is that the actual size of an int is implementation dependent. If you do what Xai suggested and go with an 80-byte array, that would be plenty big enough.

This topic is closed to new replies.

Advertisement