SUM STRINGS OF 20 DIGITS

sum meganumbers of 20 digits

Page 1 of 1

6 Replies - 2994 Views - Last Post: 11 September 2007 - 03:30 PM Rate Topic: -----

#1 bortiz0823  Icon User is offline

  • New D.I.C Head

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

SUM STRINGS OF 20 DIGITS

Post icon  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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 bortiz0823  Icon User is offline

  • New D.I.C Head

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

Re: SUM STRINGS OF 20 DIGITS

Posted 08 September 2007 - 05:32 PM

View PostAmadeus, 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

Was This Post Helpful? 0
  • +
  • -

#5 bortiz0823  Icon User is offline

  • New D.I.C Head

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

Re: SUM STRINGS OF 20 DIGITS

Posted 09 September 2007 - 11:09 AM

Any Sugestion??????
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 bortiz0823  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1