Kemoshu
Kemoshu

Reputation: 1

Large integer multiplication isn't correct

This I s what I got so far. Overall, I just need the multiplication fix small number are good when outputting. Large numbers are displaying the wrong output.

cout << "Enter an integer";
int finteger; cin >> finteger;
cout << "Enter another integer";
 int sinteger; cin >> sinteger;
int addition = finteger + sinteger;
int subtraction = finteger - sinteger;
int multiplication = finteger * sinteger;
//Outputting the answers along with the equations
cout << finteger << " + " << sinteger << " = " << addition << endl;
cout << finteger << " - " << sinteger << " = " << subtraction << endl;
cout << finteger << " * " << sinteger << " = " << multiplication << endl;

Upvotes: 0

Views: 375

Answers (1)

Bathsheba
Bathsheba

Reputation: 234635

Most likely, the multiplied result is outside the range of an int on your platform. That's undefined behaviour. On some platforms the range can be as small as -32767 to +32767.

Despite all the advances over the years C++ still remains a ridiculously difficult language in which to perform even the simplest mathematical calculations without undefined behaviour creeping in somewhere.

The fact is that all your expressions are vulnerable to overflow!

If you're building a calculator from standard input then probably the simplest thing to do is to use a multi precision library such as the one that's part of the Boost distribution. www.boost.org.

Upvotes: 1

Related Questions