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

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;

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.

ok, my code looks like this:

#include<iostream>
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<j;i++){
    cin>>n;
    number[i]=n;
    }
    for(int i=0;i<j;i++){
        cout<<number[i];
    }



return 0;

but how do i enter number without space;
i would like to enter 12345679, now i have to enter 1 2 3 4 5 6 7 9

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You can read in a string like this :

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

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!

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


p.s. sorry, i will use code tags

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;
}
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
}

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

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

This question has already been answered. Start a new discussion instead.