# SUM STRINGS OF 20 DIGITS

Page 1 of 1

## 6 Replies - 5451 Views - Last Post: 11 September 2007 - 03:30 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=32912&amp;s=056c72fec9dd651d7162af573511276c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bortiz0823

Reputation: 0
• Posts: 21
• Joined: 28-January 07

# SUM STRINGS OF 20 DIGITS

Posted 08 September 2007 - 04:45 PM

Hi guys, I want to know what is the problem with this code. I have to make a code to SUM 2 numbers of 20 digits each numbers. Example:
11111111111111111111
22222222222222222222
-----------------------------
33333333333333333333

I dont know why when I enter more then 9 digits the code KABOOOM!!!...LOL. Any sugestion are welcome.

```#include <iostream>
#include <cstdlib>
using namespace std;
const int n=30;

void Sum( string num1[n],string num2[n])
{

int num3[n], num4[n],total[n];

num3[n]=atoi(num1[n].c_str());
num4[n]=atoi(num2[n].c_str());
total[n]=num3[n]+num4[n];

cout<<"TOTAL = "<<total[n]<<endl;

}
int main()
{

string x[n];
string y[n];
cout<<"Enter a number of 20 digits: "<<endl;
cin>>x[n];
cout<<"Enter another number of 20 digits: "<<endl;
cin>>y[n];
Sum(x,y);

system ("pause");
return 0;
}

```

This post has been edited by bortiz0823: 08 September 2007 - 04:49 PM

Is This A Good Question/Topic? 0

## Replies To: SUM STRINGS OF 20 DIGITS

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: SUM STRINGS OF 20 DIGITS

Posted 08 September 2007 - 05:26 PM

Well, for starters, you are trying to place the user input into a single element of each array...I assume you are actually trying to place the input into the array as a whole, correct?

### #3 BrainStew

Reputation: 1
• Posts: 154
• Joined: 02-August 07

## Re: SUM STRINGS OF 20 DIGITS

Posted 08 September 2007 - 05:29 PM

you want to use the cin.getline() function to recieve the users entire input.

### #4 bortiz0823

Reputation: 0
• Posts: 21
• Joined: 28-January 07

## Re: SUM STRINGS OF 20 DIGITS

Posted 08 September 2007 - 05:32 PM

Amadeus, on 8 Sep, 2007 - 05:26 PM, said:

Well, for starters, you are trying to place the user input into a single element of each array...I assume you are actually trying to place the input into the array as a whole, correct?

Yep. Exactly. I dont know if thats correct but, I try to convert the all the string on a INT to sum it.

This post has been edited by bortiz0823: 08 September 2007 - 05:46 PM

### #5 bortiz0823

Reputation: 0
• Posts: 21
• Joined: 28-January 07

## Re: SUM STRINGS OF 20 DIGITS

Posted 09 September 2007 - 11:09 AM

Any Sugestion??????

### #6 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: SUM STRINGS OF 20 DIGITS

Posted 09 September 2007 - 12:27 PM

well if the strings are composed of same number then just get the first char of the string and convert it into int, do the same with the other string, calculate the sum, and the convert the int into string with the length same with the other strings, but then the problem would be the case with adding 77777 with 888888.... my suggestion is limited to the sum of 99999999999999

### #7 bortiz0823

Reputation: 0
• Posts: 21
• Joined: 28-January 07

## Re: SUM STRINGS OF 20 DIGITS

Posted 11 September 2007 - 03:30 PM

Hi guys, I start again creating a new code and I have a little problem. I made this code only to show the two numbers of 20 digits. The code run fine but, the second number show a error of digits. I dont know why because I use the same logistics. If any can help me, I apreciated. Thanks!
```//This program show 2 numbers of 20 digits
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{

char numA[ 20 ];
char numB[ 20 ];
int num2[ 20 ];
int num3[ 20 ];
char aa, bb;	//Var. Temp.

cout<<"Entre un numero de 20 digitos: ";
cin>>numA;

for( int ii = 0; ii < 20; ii++ )
{
aa = numA[ ii ]; //Conversion
num2[ ii ] = atoi( &aa ); //Conversion
}

cout<<"\nEntre otro numero de 20 digitos: ";
cin>>numB;

for( int ii = 0; ii < 20; ii++ )
{
bb = numB[ ii ]; //Conversion
num3[ ii ] = atoi( &bb ); //Conversion

}

cout << "\nNumber 1: ";

for( int i = 0; i < 20; i++ )
{
cout << num2[ i ];
}

cout << "\nNumber 2: ";

for( int i = 0; i < 20; i++ )
{
cout << num3[ i ];
}

cout<<"\n";
system("pause");
return 0;

}

```