without any upper bound unlike int
Be careful with such things. There will always be some upper bound with any solution, at the very least when your machine's memory is exhausted. A robust application should always have some kind of error checking.
If i simply add two string, i can't get correct result.
Well, that's obvious, isn't it? String concatentation doesn't know anything about mathematical semantics.
I thought of converting string to int and then performing addition,
but long string can't be converted to int.
Exactly. Internally converting the string to a built-in type would defeat the whole purpose of the solution.
How can i define my own way of adding two strings so that i get the
desired result?
The goal is apparently to support numbers bigger than what the built-in types provide.
First of all, are you really sure that your application needs to work with such huge numbers? Even a standard int should usually be more than enough, not to mention long long (standard since C++11 but practically usable even before that).
Perhaps what you really need is to detect invalid user input like "10000000000000000000000000".
String streams provide this error detection for you. Here is a complete example for you to play with, including exemplary usage of std::numeric_limits:
#include <iostream>
#include <stdexcept>
#include <exception>
#include <limits>
int ReadInt()
{
int result;
std::cin >> result;
if (!std::cin)
{
throw std::runtime_error("Illegal number");
}
return result;
}
int main()
{
try
{
std::cout << "Enter number (max: " << std::numeric_limits<int>::max() << ") > ";
int input = ReadInt();
std::cout << "You entered the following number: " << input << "\n";
}
catch (std::exception const &exc)
{
std::cerr << exc.what() << "\n";
}
}
Here are three example runs on my machine. The first with a "normal" small number, the second just barely larger than the maximum possible, the third exactly the largest possible integer:
Enter number (max: 2147483647) > 1000
You entered the following number: 1000
Enter number (max: 2147483647) > 2147483648
Illegal number
Enter number (max: 2147483647) > 2147483647
You entered the following number: 2147483647
Now, if you really really must support large integer numbers internally, don't reinvent the wheel. Use Boost.Multiprecision:
http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/index.html
Since the documentation of that particular library may be a bit hard to swallow, here is an ultra-simple example to get you started:
#include <iostream>
#include <stdexcept>
#include <exception>
#include <boost/multiprecision/cpp_int.hpp>
int main()
{
try
{
boost::multiprecision::int128_t number("100000000000000000000000000000000");
number *= 2;
std::cout << number << "\n";
}
catch (std::exception const &exc)
{
std::cerr << exc.what() << "\n";
}
}
This actually prints 200000000000000000000000000000000.
long long intorunsigned long long int. It's unlikely that you'll need a variable longer than that, to hold a number. There's alsolong double.long long intetc. If a programmer thinks a user might type an unusually long number, then I feel the algorithm should be more focused on splitting the number into more workable components (using number datatypes) which can perform calculations faster than a string, rather than use the number as a string and try performing calculations on it.intmax_t, likestd::vector<intmax_t>would be more suitable. String seems entirely the wrong container for this work.