•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,600 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,438 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4786 | Replies: 10
![]() |
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
I need some help, heh, i have to store a twelve digit number somewhere... as i've understood it nothing like unsigned int or long double or anything won't work
have i missed something? or can anyone point me to anything which i can use to be able to do this?... i have to be able to performe calculations on that number too... 
i'm using c++ by the way.
have i missed something? or can anyone point me to anything which i can use to be able to do this?... i have to be able to performe calculations on that number too... 
i'm using c++ by the way.
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by Mr A
I need some help, heh, i have to store a twelve digit number somewhere... as i've understood it nothing like unsigned int or long double or anything won't workhave i missed something? or can anyone point me to anything which i can use to be able to do this?... i have to be able to performe calculations on that number too...
i'm using c++ by the way.
Not the most trivial program. We had a similiar problem like this in a programming content many years ago. I still have the source, so take a long, and it should help. It stores big numbers, up to 100 numbers if I remember correctly. There is no type for what you want to do. You gotta use strings.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include <fstream.h>
void Simulation();
void Reverse( int * );
void PrintNums( int * );
void NullifyMemory( char *, char * , char *, int * );
void FillArrays( char *, char *, char * );
void ComputeSum( char *, char *, int * );
enum Restrictions { FRestriction = 50, SRestriction, TRestriction = 101 };
const int ModFactor = 10;
ifstream infile("E:\\largenum.txt");
int main()
{
Simulation();
system("PAUSE");
return 0;
}
void Simulation()
{
char nums[ TRestriction ] = { '\0' };
char num1[ FRestriction ] = { '\0' };
char num2[ FRestriction ] = { '\0' };
int sum[ SRestriction ] = { 0 };
while ( infile.getline( nums, TRestriction, '\n' ) )
{
FillArrays( nums, num1, num2 );
ComputeSum( num1, num2, sum );
Reverse( sum );
PrintNums( sum );
NullifyMemory( nums, num1, num2, sum );
}
}
void Reverse( int sArr[] )
{
int temp = 0;
int limit = FRestriction;
for ( int i = 0; i < SRestriction / 2; ++i )
{
temp = sArr[ i ];
sArr[ i ] = sArr[ limit ];
sArr[ limit-- ] = temp;
}
}
void PrintNums( int outArr[] )
{
bool FoundNonZero = false;
for ( int i = 0; i < SRestriction; i++ )
{
if ( outArr[ i ] > 0 )
FoundNonZero = true;
if ( FoundNonZero )
cout << outArr[ i ];
}
cout << endl;
}
void NullifyMemory( char _nums[], char _num1[], char _num2[], int _sum[] )
{
for ( int i = 0; i < TRestriction; i++ )
_nums[ i ] = '\0';
for ( int i = 0; i < FRestriction; i++ )
_num1[ i ] = _num2[ i ] = '\0';
for ( int i = 0; i < SRestriction; i++ )
_sum[ i ] = 0;
}
void FillArrays( char _nums[], char _num1[], char _num2[] )
{
int sizeString = strlen( _nums );
int i = 0;
for ( i = 0; i < FRestriction && _nums[ i ] != ','; i++ )
_num1[ i ] = _nums[ i ];
i++;
int j = 0;
while ( i <= sizeString && _nums[ i ] != '\n' )
_num2[ j++ ] = _nums[ i++ ];
}
void ComputeSum( char _num1[], char _num2[], int _sum[] )
{
int num1Counter = strlen( _num1 );
int num2Counter = strlen( _num2 );
int MaxSize = (num1Counter > num2Counter ) ? num1Counter :num2Counter;
bool CarryOver = false;
int tempSum = 0;
int walker = 0;
int s1 = 0;
int s2 = 0;
for ( walker = 0; walker < MaxSize; walker++ )
{
if ( ( num1Counter - 1 ) >= 0 )
s1 = _num1[ num1Counter-- - 1 ] - '0'; //Convert string to int
else
s1 = 0;
if ( ( num2Counter - 1 ) >= 0 )
s2 = _num2[ num2Counter-- - 1 ] - '0'; //Convert string to int
else
s2 = 0;
tempSum = s1 + s2;
if ( CarryOver )
{
_sum[ walker ] = ( ( tempSum % ModFactor ) + 1 ) % ModFactor;
if ( tempSum != 9 )
CarryOver = !CarryOver;
}
else
_sum[ walker ] = ( tempSum % ModFactor );
CarryOver = ( tempSum > 9 ) ? true : CarryOver;
}
if ( CarryOver )
_sum[ walker ] = 1;
}The input file had the following format:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX, XXXXXXXXXXXXXXXXXXXXXXXXXXXX
where X was any number. The two large numbers are delimited by a comma.
Have fun.
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by Mr A
okay thanks for the help :-) i don't understand half of what that program does but i'll give it a try and try to find out :-) tho i thought there was some kind of file or something you could include for larger numbers support... bu really i have no idea :-) thanks again
It's possible that there is a 3rd party library file. My friend wrote one. But no such file is built into the language that I'm aware of.
•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
Why can't you use a double?
double n = 123456789012.0;
A double on most machines is at least 64 bits, so there should be plenty of accuracy.
If you must use an integer, some systems (MS VC++ for example) support 64-bit integers. In VC it goes like this:
__int64 n = 123456789012i64;
note the i64 suffix on the literal value; if you left that off the compiler would complain about the number being to large for an integer.
Once you have your number in this format, you can use it in integer math.
double n = 123456789012.0;
A double on most machines is at least 64 bits, so there should be plenty of accuracy.
If you must use an integer, some systems (MS VC++ for example) support 64-bit integers. In VC it goes like this:
__int64 n = 123456789012i64;
note the i64 suffix on the literal value; if you left that off the compiler would complain about the number being to large for an integer.
Once you have your number in this format, you can use it in integer math.
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
hmm... when i make it a double it says "error: integer constant is too large
for "long" type"
are you sure about 64bits? well it doesn't work
i'm running MinGW if that helps anything...
btw the __int64 gave the same errormessage
but hey it must be doable? ... i mean some kindof file i can include?
thanks for all the help anyways
for "long" type"
are you sure about 64bits? well it doesn't work
i'm running MinGW if that helps anything...btw the __int64 gave the same errormessage

but hey it must be doable? ... i mean some kindof file i can include?

thanks for all the help anyways
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Store large numbers in C (C++)
Other Threads in the C++ Forum
- Previous Thread: ambigious part 5
- Next Thread: counting comparisons when sorting


Linear Mode