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.

## All 13 Replies

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

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

You can read in a string like this :

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

but getline only works with letters not with numbers

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:)

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 :)

but getline only works with letters not with numbers

strange... what if my name is U2 ?

commented: lol +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.