-
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
-
you wouldn't want to do that. not with pointers.
use strings. they're safe.
-
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.
-
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;
}
-
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
-
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());
}
-
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;
}
-
Thanks to everyone who replied!
Appreciated!
Now working fine