0

I've made a simple c++ program that takes in two numbers, clears the screen, and then outputs the sum of those two numbers to the terminal window. It works great. Typing 5 as the first number and 10 as the second outputs 15.

Except there's one issue. When running the program with the first number being 5 and the second being something very long, such as:

3285720358162039587169230839461283795732948567123857690,

the program doesn't output the right number, and doesn't show the correct number that the user typed either.

For example, when using the previous two numbers, the "sum" will be: -2147483644, when it should be the long number with a 5 instead of a zero on the end.

Here's my code:

#include <iostream>

int main() {
    system ("clear");
    int num1 = 0;
    int num2 = 0;

    std::cout << "What is the first number?: ";
    std::cin >> num1;

    std::cout << "What is the second number?: ";
    std::cin >> num2;

    system ("clear");

    std::cout << "The sum of " << num1 << " and " << num2 << " is: " << (num1 + num2) << "\n";
}
  • 1
    `int`egers have a minimum (`INT_MIN`) and maximum (`INT_MAX`) value. If you check `INT_MAX` you'll find that it's a lot less than 3285720358162039587169230839461283795732948567123857690. It's very often 2147483647. – Ted Lyngmo Aug 14 '22 at 17:48
  • 1
    `INT_MAX` is typically a little over 2 billion. A [decent text-book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) should have mentioned that all native types are limited in C++, there are no native types which can handle arbitrarily large values. – Some programmer dude Aug 14 '22 at 17:52
  • @Someprogrammerdude I see. Can you explain why? I just tried it, and python can do it. Is it just the way that c++ was designed? – XboxOneSogie720 Aug 14 '22 at 17:56
  • @XboxOneSogie720 Actually it was the way that C was designed, and C++ inherited this from C – john Aug 14 '22 at 17:58
  • @XboxOneSogie720 Python uses a BigNum library internally. You can search for a library to use with C++ that handles big numbers. – Ted Lyngmo Aug 14 '22 at 17:58
  • @XboxOneSogie720 Of course you can write C++ code that allows you to use large integers, it's just not built into the language, see [GMP](https://gmplib.org/) for example – john Aug 14 '22 at 17:59
  • 1
    Python is specifically made to handle arbitrarily large integers. The native integer types of C++ tend to match the CPU machine word lengths, so `short` is typically `16` bits, `int` is `32` bits, `long` tend to be either `32` or `64` bits, and `long long` is at least `64` bits. Since the sizes are limited, then so are their values. – Some programmer dude Aug 14 '22 at 17:59
  • I tried the computation on Lua 5.3 for comparison. The result is `3.285720358162e+54`, which is not wrong, but approximate. Because the value is too big for a 64-bit integer, Lua handled the value as a `double` floating point. – prapin Aug 14 '22 at 18:02
  • @Someprogrammerdude Thanks for clarification. I guess it makes sense that the computer can't handle more than what it's built for, and that in order to handle larger numbers, extra code to deal with those bigger digits is required. – XboxOneSogie720 Aug 14 '22 at 18:06
  • Search the internet for "C++ big number library". See what the maximum integer value that the library supports. – Thomas Matthews Aug 14 '22 at 18:09

1 Answers1

-2

There are limits on integer size. ~~You're probably going to declare the numbers as a long long.~~

EDIT: Ted is correct, long longs won't be large enough for that particular input either.

casr
  • 77
  • 6
  • `long long` won't get OP very far either. I don't think there's an implementation with a `long long` that can hold values bigger than 9223372036854775807 around. – Ted Lyngmo Aug 14 '22 at 17:54
  • @TedLyngmo You are correct, I've added a note – casr Aug 14 '22 at 17:57
  • the page you link is somewhat irrelevant, because it lists ranges of some windows fixed width types. For integers the standard merely requires a minimum width, no maximum width. – 463035818_is_not_an_ai Aug 14 '22 at 18:25