0

How to add or subtract the value of string? For example:

 std::string number_string; std::string total; cout << "Enter value to add"; std::getline(std::cin, number_string; total = number_string + number_string; cout << total; 

This just append the string so this won't work. I know I can use int data type but I need to use string.

11
  • What do you mean by "adding" strings? Do you mean you have strings that contain numbers and you want to add those numbers? Show us some example inputs and the output you'd like to see. Commented Dec 21, 2012 at 20:48
  • I mean adding like 1+3=4 ... Commented Dec 21, 2012 at 20:48
  • 2
    Why do you want to use a string to do math? Is it because an int is too small to hold the values you want to calculate? In which case, you should investigate "bignum" libraries. Commented Dec 21, 2012 at 20:49
  • C++ does not have any built-in functions to do what you ask. You need to write your own. Commented Dec 21, 2012 at 20:49
  • 3
    Are you looking to add small (less than 10 digits or so) numbers, or very large numbers (more than about 10 digits)? Commented Dec 21, 2012 at 20:52

2 Answers 2

2

You can use atoi(number_string.c_str()) to convert the string to an integer.

If you are concerned about properly handling non-numeric input, strtol is a better choice, albeit a little more wordy. http://www.cplusplus.com/reference/cstdlib/strtol/

Sign up to request clarification or add additional context in comments.

12 Comments

strtol(3) is probably preferred over atoi(3) since it can return an error (via errno).
Good hint, amended answer.
@sftrabbit: not sure I agree that it's wise to throw C++11 answers at a newbie this year. Ask me again in 2015.
First I thought to downvote since there really should be no reason to use a c construct (both quite flawed in their own unique way), but then I remembered how horrible the c++ solution is itself, so carry on +1
@David Having to include boost for parsing an integer is hardly a great solution for a beginner. So the C++ way before c++11 would be std::string s = "100"; int i; std::istringstream ss(s); ss >> i; char c; if (ss.fail() || ss.get(c)) // not a number - which must be the most horrible int parsing code in any higher level language. Let's hope that c++11 becomes more widespread that we can forget this stuff.
|
1

You will want to work with integers the entire time, and then convert to a std::string at the very end.

Here is a solution that works if you have a C++11 capable compiler:

#include <string> std::string sum(std::string const & old_total, std::string const & input) { int const total = std::stoi(old_total); int const addend = std::stoi(input); return std::to_string(total + addend); } 

Otherwise, use boost:

#include <string> #include <boost/lexical_cast.hpp> std::string sum(std::string const & old_total, std::string const & input) { int const total = boost::lexical_cast<int>(old_total); int const addend = boost::lexical_cast<int>(input); return boost::lexical_cast<std::string>(total + addend); } 

The function first converts each std::string into an int (a step that you will have to do, no matter what approach you take), then adds them, and then converts it back to a std::string. In other languages, like PHP, that try to guess what you mean and add them, they are doing this under the hood, anyway.

Both of these solutions have a number of advantages. They are faster, they report their errors with exceptions rather than silently appearing to work, and they don't require extra intermediary conversions.

The Boost solution does require a bit of work to set up, but it is definitely worth it. Boost is probably the most important tool of any C++ developer's work, except maybe the compiler. You will need it for other things because they have already done top-notch work solving many problems that you will have in the future, so it is best for you to start getting experience with it. The work required to install Boost is much less than the time you will save by using it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.