Thread: input an intiger to a string

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87

    input an intiger to a string

    hi guys i wanna put an intiger into a string for example:
    Code:
    string name;
    int num=8;
    name= "George"
    //i want in the end of George to pas the num variable
    i ve seen some examples but ive not yet come up with a solution
    like str.insert();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you seen example usage of a stringstream?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    xmm not really i will do search now

  4. #4
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    thanks find out the solution
    Code:
    int main () {
      int num=4;
      std::stringstream ss;
      ss.str ("George_");
      std::string s = ss.str();
      std::cout << s << num<<endl;
      system("pause");
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    but how can i pass this resualt to a string for example:
    Code:
    int main () {
      int num=4;
      stringstream ss;
      ss.str ("George_");
      string s = ss.str();
      string n = s << num;
      system("pause");
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You've almost got it. Take a look how you can use stringstream.
    Code:
    int main()
    {
        string name = "George";
        int num = 4;
        stringstream ss;
        ss << name << "_" << num;
        string result = ss.str();
        cout << result << '\n';
        system("pause");
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    ooo yea now i see thenx very much

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int n = 4;
    std::string s = "George_" + std::to_string(n);
    If your compiler supports C++11.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 02-27-2013, 03:45 PM
  2. Replies: 1
    Last Post: 02-07-2013, 05:02 AM
  3. Replies: 10
    Last Post: 12-09-2011, 02:52 AM
  4. Replies: 13
    Last Post: 11-04-2011, 06:16 PM
  5. Intiger to float
    By PvtVampire in forum C Programming
    Replies: 9
    Last Post: 10-07-2011, 05:55 AM