I have to creat a program where the user inputs two large ints, and they are converted into string in order to add them together (assuming they are larger than 20 characters). One thing, is that it tells me to convert the numbers to a string and store it in reverse order....how do I do this?

Dani AI

Generated

A few quick, practical points that build on what and others started: treat each big integer as a string from the start (as suggested). Reversing the string just makes digit-by-digit addition easier because the least significant digit becomes index 0 and you can loop forward with a single carry variable.

Basic workflow

  • Read the numbers as strings (operator>> skips whitespace).
  • Validate and strip an optional leading '+' (handle '-' separately if negatives are required).
  • Create a reversed copy with string rev(x.rbegin(), x.rend()) or std::reverse(x.begin(), x.end()).
  • Loop i = 0..max(lenA,lenB)-1, take digit = (i < len ? s[i]-'0' : 0), sum with carry, append char('0' + sum%10) to result.
  • After the loop, if carry remains append it, then reverse the result back to normal order and trim leading zeros.

Minimal C++ example (keeps reversed storage while adding):

#include <iostream>
#include <string>
#include <algorithm>

std::string addBig(std::string a, std::string b) {
  std::reverse(a.begin(), a.end());
  std::reverse(b.begin(), b.end());
  std::string r;
  int carry = 0;
  size_t n = std::max(a.size(), b.size());
  for (size_t i = 0; i < n; ++i) {
    int da = (i < a.size() ? a[i]-'0' : 0);
    int db = (i < b.size() ? b[i]-'0' : 0);
    int s = da + db + carry;
    r.push_back(char('0' + (s % 10)));
    carry = s / 10;
  }
  if (carry) r.push_back(char('0' + carry));
  std::reverse(r.begin(), r.end());
  size_t p = r.find_first_not_of('0');
  return p==std::string::npos ? "0" : r.substr(p);
}

int main() {
  std::string a, b;
  if (!(std::cin >> a >> b)) return 0;
  std::cout << addBig(a, b) << "\n";
}

Notes and pitfalls

  • This code does not handle negatives; to support subtraction, compare absolute values and subtract as needed.
  • For huge inputs avoid unnecessary copies (use reverse iterators to add without creating reversed copies).
  • Validate input characters to reject non-digits and decide how to handle leading zeros.

This approach is O(n) time and straightforward to test with small cases before trying huge inputs. and pointed toward conversion and code resources; the string-first approach avoids overflow and is simpler for arbitrary-length arithmetic.

Recommended Answers

All 6 Replies

i didn't understand it to 100%, but you can convert number to string very easy with stringstream()

I have to creat a program where the user inputs two large ints, and they are converted into string in order to add them together (assuming they are larger than 20 characters). One thing, is that it tells me to convert the numbers to a string and store it in reverse order....how do I do this?

Thats not smart. How about just getting the inputs as a string and
avoid the conversion? Try looking at the code snippet here.

Thats not smart. How about just getting the inputs as a string and
avoid the conversion? Try looking at the code snippet here.

I don't see the code snippet....

There are code snippets on daniweb. I just can't find it atm, gimme a sec to check.

edit: Found it:
http://www.daniweb.com/code/

works also fine!

I don't see the code snippet....

Its on the right side of the webpage, here is the C++ section.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.