Thread: concatenate an integer to a string?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    concatenate an integer to a string?

    hi,

    i'm using msvc++.

    i think in c++ i can use the phrase "string string_A;" and i simply declare string_A of type string...like how char works. is this true?

    also, i'm writing a program that concatenates a number after a word. when i try to compile with
    Code:
    strcat(link, num);
    , where link is a array of characters and num is an integer...and i get a compile error:

    C:\progone.cpp(36) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'

    so how can i concatenate an integer to a string?

    thanks,
    barneygumble742

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at the prototype for strcat. It wants a string, not a number for the second argument. You can't just place whatever you feel like for any argument to any function. You have to pass what it expects you to pass it.

    That being said, if you insist on using the older C style functions, consider using sprintf. Or just use the string class.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To use the C++ string class you must #include<string> and call it std::string or add a using namespace std; to your code.

    To concatenate an integer to the end of a C++ string, you need to use a stringstream from <sstream>.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Using a string/stringstream combo:

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::stringstream sstr;
        int value = 5;
    
        // Insert text/integer into stringstream
        sstr << "The value is: " << value;
    
        // Convert stringstream into string
        std::string str = sstr.str();
    
        // Output string
        std::cout << str << std::endl;
    
        return 0;
    }
    Should output:
    Code:
    The value is: 5
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM