Thread: converting int to sting

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    converting int to sting

    I don't know how to convert an int to a sting in C++ and it is driving me nuts. I know this is easy. Can someone PLEASE help?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main () {
    
      int val;
      string mystr;
      stringstream ss (stringstream::in | stringstream::out);
    
      ss << "120 42 377 6 5 2000";
    
      for (int n=0; n<6; n++)
      {
        ss >> val;
        cout << val*2 << endl;
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    ummm...

    Now I really feel stupid...I have no clue what that bit of code does. Unless I'm totally wrong, that doesn't convert an int variable to a string. Specifically, I have to change the value 135 (or whatever) minutes into "2:15." I know there has to be an easy way to do this. I wish I could just use java...String.valueOf(int x).

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Oops, sorry about that, it's doing string to int. So you want int to string?

    PHP Code:
    #include <stringstream>
    #include <iostream>
    #include <string>
    using namespace std;

    string itos(int i)    // convert int to string
    {
        
    stringstream s;
        
    << i;    // sent i to the stringstream
        
    return s.str();
    }        
    // return the contents of the stream
             // converted to a string

    int main()
    {
        
    int i 127;
        
    string ss itos(i); // Our function

        
    cout << ss << endl// Print it

    Last edited by Eibro; 10-13-2002 at 08:53 PM.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    thanks alot...that was what i was looking for.

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
        int num = 15;
        char string[10];
        itoa(num, string, 10);
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    VC++

    Is Stringstream not in VC++?

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: VC++

    Originally posted by Extol
    Is Stringstream not in VC++?
    #include <sstream>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM