i want to assign a integer of 100 digit length to a object. like

class hugeint
{
///
};
void main()
{
hugeint a,b,c;
a=10000000000000000000000000000000....100;
b=20;
c=a+b;

}

can any buddy suggest me any simple idea to achieve this..

Recommended Answers

All 9 Replies

Assign it to a std::string or char array. Within your class you can design your own methods for doing operations with the numbers.

Assign it to a std::string or char array. Within your class you can design your own methods for doing operations with the numbers.

yes i know we can do it int that way.i am newbie in c++ can you give me example snippet :)

Try it out for yourself first. Adding is probably the simplest you just have to manage the carries.

Try it out for yourself first. Adding is probably the simplest you just have to manage the carries.

i m trying :)

Post down the code which you have already worked on .. And then ask for specific questions. You get lots of help on this forum in that manner :)

i used the atoi function to convert the chacter array into the number

#include<iostream.h>
#include<conio.h>
void main()
{

char take[100];
int tnt[100];
int i;
clrscr();
i=0;
for(i=0;i<100;i++)
  {
  cin>>take[i];
  }

for(i=0;i<100;1++)
   {
tnt[i]=atoi(take[i]);

 }

but it didnt work..
if get all the chachter number into integer array i will go ahead but i am stucked in this !!
getch();
}

If you have a single chr that is a digit then converting it to a number is easy

char achar = '5';
int theint = achar - '0'; // theint now has the value 5

If necessary validate that the char is a digit using isdigit

#include<iostream.h> -- old, do not use
The current header (as of the 1990's) is simply iostream #include<conio.h> -- Non-portable, not standard, do not use void main() -- main() is an int function, not a void. See this clrscr(); -- Non-portable, not standard, do not use tnt[i]=atoi(take[i]); -- Does atoi() take a single character? Look it up.

If the character is >= '0' and <= '9', it's a digit. To convert a digit to a number, subtract '0'. Each of these character are just special numbers so you can do math on them.

Thank you both of :)
it is very helpful now i am go ahead.if i will stucked in another problem i will ask you :)

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.