1,105,406 Community Members

the product of your two numbers is 373441392

Member Avatar
hilt_m
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello all, I've just started to learn C++ since I have a bit of time on my hands. I was doing an exercise, learning functions and built a multiplier (very basic, enter 2 numbers and here is the result) strange thing, if the numbers you enter are very high, I get this: the product of your two numbers is 373441392, is there a reason it's this number, anyone know?

#include <iostream>

using namespace std;

int mult ( int x, int y );

int main()
{
    int x;
    int y;

    cout<<"Please input two numbers to be multiplied (separated by a space) : ";
    cin>> x >> y;
    cin.ignore();
    cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
    cin.get();
}

int mult ( int x, int y )
{
    return x * y;
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,656 posts since Aug 2005
Reputation Points: 5,234 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
1
 

The reason is there is a maximum value that an integer can hold -- the maximum is declared in the header file limits.h that is supplied by your compiler. What you are seeing is called numeric overflow.

If you need bigger numbers then use a bigger integer, such as "long long", which holds about twice as many digits as an int or long. There are no standard C or C++ data types that will hold an infinite number of digits.

Question Answered as of 2 Years Ago by Ancient Dragon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article