CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2
  • Learn How to Create Dual Mode Windows Services

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 23rd, 2009, 04:11 PM
    momo12 momo12 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    momo12 is an unknown quantity at this point (<10)
    Exclamation is there a way to include a integer variable in a string?

    Hi,

    is there a way to include a integer variable in a string?

    Example:

    string A;
    int N;

    A = "image N" //where N will be replaced with what is in the integer variable N.

    //like when we do printf("inage %d", N) but storing the string in the variable without printing.

    Thanks
    Reply With Quote
      #2    
    Old November 23rd, 2009, 04:15 PM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,165
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    Re: is there a way to include a integer variable in a string?

    sprintf
    Reply With Quote
      #3    
    Old November 23rd, 2009, 04:32 PM
    momo12 momo12 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    momo12 is an unknown quantity at this point (<10)
    Re: is there a way to include a integer variable in a string?

    Thankyou!
    Reply With Quote
      #4    
    Old November 23rd, 2009, 07:40 PM
    Philip Nicoletti Philip Nicoletti is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 2000
    Location: West Virginia
    Posts: 6,819
    Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+) Philip Nicoletti has a brilliant future (2000+)
    Re: is there a way to include a integer variable in a string?

    It is undefined to use sprintf on a std::string ... use stringstream

    Example:

    Code:
    #include <string>
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    template <typename T1 , typename T2>
    T2 Convert(const T1 & t1)
    {
        T2 t2 = T2();
    
        std::stringstream ss;  // include <sstream>
    
        ss << t1;
    
        ss >> t2;
    
        return t2;
    }
    
    
    int main(int argc , char* argv[])
    {
        int n = 10;
    
        string s = "image ";
    
        s += Convert<int,string>(n);
    
        cout << s << "\n";
    
        return 0;
    }
    Reply With Quote
      #5    
    Old November 23rd, 2009, 07:58 PM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,165
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    Re: is there a way to include a integer variable in a string?

    Quote:
    Originally Posted by Philip Nicoletti View Post
    It is undefined to use sprintf on a std::string ... use stringstream
    You're right, I should have read the post more carefully. I thought he was asking about a char array.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:53 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.