954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to save big number into array

hi everyone ;)
i'm am having a problem, can you help me how to save big number into array?
i have to input two big numbers (20 digits or less) and sum them.
please help

harryoma
Newbie Poster
12 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Have you any code?
What are the problems you have with it?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

i have to input number, and then save that number so that evey digit is one element of array...

this is my code so far

cout<<"Input number:";
cin>>n;

harryoma
Newbie Poster
12 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

First declare an array and put your cin in a loop to read in every digit of your bignumber. Check if it's a digit and put it in the array.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

ok, my code looks like this:


#include
using namespace std;

int main(){
int number[20];
int n,j;

cout<<"How many digits have your number: ";
cin>>j;
cout<<"Input bigdigit: ";

for(int i=0;i>n;
number[i]=n;
}
for(int i=0;i

harryoma
Newbie Poster
12 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

You can read in a string like this :

string Mystr;
cout << "What's your name? ";
getline (cin, Mystr);

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

but getline only works with letters not with numbers

harryoma
Newbie Poster
12 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

You could use atoi or write your own char-to-digit converter, like this:

int char2int(char c) {
     switch(c) {
          case '0': {
               return 0;
          }
          case '1': {
               return 1;
          }
          // ... and so on
     }
}


You could also do it without the switch, but I'll let you figure out how.

P.s. Please use code tags when posting code!

mrboolf
Junior Poster
183 posts since Jun 2008
Reputation Points: 134
Solved Threads: 18
 

this looks complicated, i need simpler solution:)
please help


p.s. sorry, i will use code tags

harryoma
Newbie Poster
12 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

I hope this is not homework, so I'll show you a little more (it's simpler than it seems!)

#include <iostream>
#include <string>
using namespace std;

int char2int(char c) {
   switch(c) {
      case '0': {
         return 0;
      }
      case '1': {
         return 1;
      }
      case '2': {
         return 2;
      }
      case '3': {
         return 3;
      }
      case '4': {
         return 4;
      }
      case '5': {
         return 5;
      }
      case '6': {
         return 6;
      }
      case '7': {
         return 7;
      }
      case '8': {
         return 8;
      }
      case '9': {
         return 9;
      }
   }
}

int main() {
   string myBigNum;
   int myBigNumArray[20];
   cout << endl << "Enter the number" << endl;
   getline(cin, myBigNum);
   if(myBigNum.size()>20) {
      cout << endl << "You entered a number too big!" << endl;
      return EXIT_FAILURE;
   }
   for(unsigned int i = 0; i < myBigNum.size(); ++i) {
      myBigNumArray[i] = char2int(myBigNum[i]);
   }
   // now you have your number saved in the array
   // of course you'll need sign checking and a better input validation
   // but this is a start and it's simple
   return EXIT_SUCCESS;
}
mrboolf
Junior Poster
183 posts since Jun 2008
Reputation Points: 134
Solved Threads: 18
 
int char2int(char c) {
   switch(c) {
      case '0': {
         return 0;
      }
      case '1': {
         return 1;
      }
      case '2': {
         return 2;
      }
      case '3': {
         return 3;
      }
      case '4': {
         return 4;
      }
      case '5': {
         return 5;
      }
      case '6': {
         return 6;
      }
      case '7': {
         return 7;
      }
      case '8': {
         return 8;
      }
      case '9': {
         return 9;
      }
   }
}


Take advantage of the fact that '0' through '9' are contiguous on the ASCII chart and that you can subtract characters.

int char2int(char c)
{
     if (c < 48 || c > 57)
          return -1;  // signifies non-digit/invalid

     return c - 48;  // '0' is 48 in ASCII
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

thank you very much:)
i'm going to finsh the code- hope it will work.

harryoma
Newbie Poster
12 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

@VernonDozier: Yes, you're obviously right, I just wanted to make it as plain as possible for the OP :)

mrboolf
Junior Poster
183 posts since Jun 2008
Reputation Points: 134
Solved Threads: 18
 
but getline only works with letters not with numbers


strange... what if my name is U2 ?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You