Like Tree3Likes

Convert Integer to String and String to Integer

This is a discussion on Convert Integer to String and String to Integer within the C++ Programming forums, part of the General Programming Boards category; Who can help me, how to convert Integer to String and String to Integer, please. I have search some topic, ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Convert Integer to String and String to Integer

    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

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Take a look at the atoi() function. and the cstdlib header file.
    Last edited by AndrewHunter; 09-14-2011 at 09:18 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by hqt View Post
    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.
    Cannot use it == Not allowed to use it ?
    OR lazy to not learn about it ?

    In the first case, just run a loop and get the numbers in the places by subtracting '0' multiply with the relevant power of 10 before adding.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by AndrewHunter View Post
    Take a look at the atoi() function. and the cstdlib header file.
    Does those work on std::string s ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by manasij7479 View Post
    Does those work on std::string s ?
    With the std::string.c_str() member, yes it does.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by hqt
    I have search some topic, thay say about stringstream, but I cannot use it.
    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?

    Quote Originally Posted by AndrewHunter
    Take a look at the atoi() function. and the cstdlib header file.
    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.
    AndrewHunter likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    "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:
    Code:
    string str="1234ha23";
    int i=atoi(str.c_str()); 
    //i=1234;
    But I don't know purpose of c_str() function and I don't know why, str must be string, not char.
    @: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.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    "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
    That means you didn't learn how to do it with stringstream. Take a look at a concise stringstream example.

    Quote Originally Posted by hqt View Post
    but now, I can do it now, by another way I newly have learned:
    Code:
    string str="1234ha23";
    int i=atoi(str.c_str()); 
    //i=1234;
    But I don't know purpose of c_str() function
    It converts a C++ std::string to a c-style string, e.g., a null terminated char array.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    So, if I don't mistake again, atoi() function can convert string to integer and integer to string, too

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by hqt View Post
    So, if I don't mistake again, atoi() function can convert string to integer and integer to string, too
    The opposite is itoa()
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    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);
    }
    manasij7479 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    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();

    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);
    }
    2) at line:
    Code:
     std::stringstream ss(std::stringstream::in | std::stringstream::out);
    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?

    thanks

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Quote Originally Posted by manasij7479 View Post
    The opposite is itoa()
    itoa is not standard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Elysia View Post
    itoa is not standard.
    Thanks.. didn't know that.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    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();
    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);
    }
    Last edited by AndrewHunter; 09-15-2011 at 08:38 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to convert from string to integer
    By vopo in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 05:32 AM
  2. convert string into integer using recursion
    By peter_hii in forum C++ Programming
    Replies: 18
    Last Post: 08-23-2006, 10:09 AM
  3. convert from integer to string
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2006, 09:28 AM
  4. how do i convert a string to an integer??
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 08:21 PM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21