You are essentially misusing C++. You have high-level functionality for all what you are trying to do.
If you want to be portable to work with old compilers, I would use sstringstream
for the conversion, otherwise see below.
string MyAdd(int A, char B)
{
stringstream ss;
ss << A << B;
return ss.str();
}
That being said, I would personally really utilize C++11 here with to_string
, so I would be writing something like this:
string MyAdd(int A, char B)
{
return to_string(A) + B;
}
See my complete test code below.
main.cpp
#include <string>
#include <iostream>
using namespace std;
string MyAdd(int A, char B)
{
return to_string(A) + B;
}
int main()
{
cout << MyAdd(0, 'A') << endl;
return 0;
}
Build and run
g++ -std=c++11 main.cpp && ./a.out
Output
0A
========================================================================
Here, you can find my pre-c++11 version if you need it for compatiblity:
main.cpp
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
string MyAdd(int A, char B)
{
stringstream ss;
ss << A << B;
return ss.str();
}
int main()
{
cout << MyAdd(0, 'A') << endl;
return 0;
}
Build and run
g++ main.cpp && ./a.out
Output
0A