Converting int to string
-
I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks
-
I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks
J_E_D_I wrote:
int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str());
Should use
atoi
().J_E_D_I wrote:
I am not able to do the opposite, eg convert an int into a string.
sprintf
()J_E_D_I wrote:
char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";}
Use single quote.
if(character == 'a') {
cout << "It is a";
}Maxwell Chen
-
I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks
J_E_D_I wrote:
but I am not able to do the opposite, eg convert an int into a string. Any help please?
Another method in addition to Maxwell's sprintf()...
#include <sstream>
...
int SomeInt = 5;std::ostringstream NumericStringStream;
NumericStringStream << SomeInt;
std::string NumericString = NumericStringStream.str();
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks
itoa
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks
For Question One :- would wsprintf or CString::format is of any use!
J_E_D_I wrote:
Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";}
use ' instead of "
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>