Thread: char* and int.

  1. #1
    Main Man
    Guest

    char* and int.

    i am pretty new to c++ and have ran into a problem.

    i have:

    int TheInt = 5;
    char* TheChar = "WORD";

    how can i add the integer to the end of the char*?

    so i get the result WORD5

    any help would be great, and also if you could explain how it works i'd appreciate it.

    Thanks

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    you wouldn't want to do that. not with pointers.

    use strings. they're safe.

  3. #3
    Main Man
    Guest
    Ok.

    Now i have:

    int TheNumber = 5;
    string TheString = "WORD"

    How can i get the result WORD5 as stated above, and also convert that result to be used in GetPrivateProfileString().

    Any Help Appreciated.

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    Maybe there're better ways of doing this (adding an
    int to a string) but I do it this way and it seems to work fine:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	int TheInt = 5;
    	char buf[20];
    	string Mystring = "WORD";
    	
    	Mystring = Mystring + itoa(TheInt, buf, 10);
    	
    	cout << Mystring;
    	
    	return 0;
    }
    Last edited by moonwalker; 07-22-2003 at 09:07 AM.

  5. #5
    MainMan
    Guest
    Hi

    Thanks for replying again.

    I tried that and the conversion works fine.
    But the end result needed is "char*" as use the result in GetPrivateProfileString().

    I got error:

    Code:
    error C2664: 'GetPrivateProfileStringA' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const
     char *'
    thanks

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You can also use the stringstream classes:

    Code:
    #include <iostream>
    #include <sstream>
    
    void display(const char * c){
    	std::cout << c << std::endl;
    }
    
    
    int main(){
    	int i(5);
    	std::stringstream s("WORD");
    	s << i;
    	display(s.str().c_str());
    }

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <iostream>
    #include <sstream> 
    #include <string> 
    using namespace std;
    
    int main(void)
    { 
        int thenumber = 5;
        string thestring = "WORD";
        stringstream ss;
        string str;
        
        ss << thenumber;
        ss >> str; 
        
        thestring += str;
        
        cout << thestring << endl;
        system("PAUSE");
        return 0;
    }

  8. #8
    Main Man
    Guest
    Thanks to everyone who replied!
    Appreciated!

    Now working fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM