I have a question about strings, specifically CStrings in visual c++ MFC apps. I was wondering how to convert ints and other numbers so i can concatenate them with other CStrings. Is there any kind of function to do this?
I have a question about strings, specifically CStrings in visual c++ MFC apps. I was wondering how to convert ints and other numbers so i can concatenate them with other CStrings. Is there any kind of function to do this?
If you want to concatenate an int with a string, you want your int to be a string.
I beleive you're looking for...
atoi for string to int, and its related functions for floats etc.
Last edited by Azuth; 05-22-2002 at 01:26 AM.
Demonographic rhinology is not the only possible outcome, but why take the chance
If you want a variable to act like another then you can use type casting. I believe it is explained in the tutorial on the cprogramming home page.
convert everything to string...
with MFC:
str is : "Do I want $100?"Code:int my_int = 100; char my_char = '$'; char* my_str = "I want "; CString str; str.Format("Do %s%c%d?", my_str, my_char, my_int); // just like printf
with STL:
str is : "Do I want $100?"Code:int my_int = 100; char *my_char = '$'; char my_str = "I want "; strstream ss; ss << "Do " << my_str << my_char << my_int << '?' << ends; string str(ss.c_str());
with C style:
please use sprintf.
sorry, a bug ... I debug it. Edited.
Last edited by Hotman_x; 05-22-2002 at 08:52 AM.
Hello, everyone.
This is what I usally do
Code:CString csTemp; int iEggs = 5; csTemp.Format("I want %d eggs",iEggs); MessageBox(csTemp);
Supplement:
convert everything to string with Boost Template Library(www.boost.org):
str is : "I want $100?"Code:int my_int = 100; char my_char = '$'; char *my_str = "I want "; string str = lexical_cast<string>my_str + lexical_cast<string>my_char + lexical_cast<string>my_int + "?";
Hello, everyone.