Who can help me, how to convert Integer to String and String to Integer, please.
I have search some topic, thay say about stringstream, but I cannot use it.
thanks
Who can help me, how to convert Integer to String and String to Integer, please.
I have search some topic, thay say about stringstream, but I cannot use it.
thanks
Take a look at the atoi() function. and the cstdlib header file.
When you say you cannot use it, do you mean that you don't know how to use it, or do you mean that you are forbidden to use it? If the latter, why?Originally Posted by hqt
If you want to use the utilities from <cstdlib>, consider strtol instead of atoi due to better error checking. However, the use of stringstreams would be the typical C++ technique.Originally Posted by AndrewHunter
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"I have search some topic, they say about stringstream, but I cannot use it" -->just simple I code as they say, and ... have some error
but now, I can do it now, by another way I newly have learned:
But I don't know purpose of c_str() function and I don't know why, str must be string, not char.Code:string str="1234ha23"; int i=atoi(str.c_str()); //i=1234;
@:manasil: I don't think your solution is the best idea because It's not effective. (and too long, for small work, I think)
Last edited by hqt; 09-14-2011 at 10:17 PM.
That means you didn't learn how to do it with stringstream. Take a look at a concise stringstream example.
It converts a C++ std::string to a c-style string, e.g., a null terminated char array.
So, if I don't mistake again, atoi() function can convert string to integer and integer to string, too
Look, if you are going to be converting formats like this then there is no reason not to learn how to use the stringstream object. This would be the correct way of doing things in C++. Something like:
Code:#include <iostream> #include <sstream> #include <string> int main(void){ int num1; //our integer std::string myphrase; //our phrase /*string stream object*/ std::stringstream ss(std::stringstream::in | std::stringstream::out); std::cout<<"Enter a number: "; std::cin>>num1; num1++; ss << num1; //Place number into our stringstream object myphrase = "One greater than that is " + ss.str(); //convert it to a string and append std::cout<< myphrase <<std::endl; return(0); }
1)Uhmmm, I don't know why this below code look like with you, but It cannot run. I met error at line
myphrase = "your number is" + ss.str();
2) at line:Code:#include<stdio.h> #include<string> #include<sstream> using namespace std; int main(){ int number=567; string myphrase; /*string stream object*/ std::stringstream ss(std::stringstream::in | std::stringstream::out); ss<<number; myphrase = "your number is" + ss.str(); printf("%s\n",myphrase); return(0); }I have read your page give me and see that is stringstream's structure. But I don't know, what mean of this structure. So, can you tell me more about this, please?Code:std::stringstream ss(std::stringstream::in | std::stringstream::out);
thanks
I am willing to bet you are getting an error at the line below that, when you try to use printf to print a string object to the screen. Regardless, I have made the following changes to the code you posted with comments. If it doesn't compile and run then there is something wrong with your compiler.
Code:#include<iostream> //<-----include this /*#include<stdio> not THIS*/ #include<string> #include<sstream> using namespace std; /*you are invoking the std namespace here globally, hence you do not need to qualify std anywhere*/ int main(){ int number=567; string myphrase; /*string stream object. Created for both input and output*/ stringstream ss(stringstream::in | stringstream::out); ss<<number; myphrase = "your number is " + ss.str(); cout << myphrase << endl; //print with cout, this is C++ /*printf("%s\n",myphrase); <---WHY would you use printf here?*/ return(0); }