Page 1 of 1

## 6 Replies - 713 Views - Last Post: 04 December 2012 - 07:12 AMRate 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=302091&amp;s=485279ead9766f1ceeeac66f6cd7f3f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Speakeasys

Reputation: 0
• Posts: 35
• Joined: 19-September 12

Posted 29 November 2012 - 01:39 PM

So I'm suppose to take in a number 20 digits or less as char and convert to int. Do I do this with a bunch of if-else statements? I have the cin as int so i'm just confused how to go about doing this. Thank you in advance. Here's what I have so far.

```

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include <sstream>

int const SIZE = 21;
using namespace std;

void inputValue1(long int[]);
void inputValue2(long int[]);
void Sum(long int array1[], long int array2[]);

main()
{
long int A[SIZE];
long int B[SIZE];
long int C[SIZE];

inputValue1(A);
inputValue2(B)/>;
Sum(A, B)/>;

getch();
}

void inputValue1(long int A[])
{
cout << "First array" << endl;
for(int i=0; i<SIZE; i++)
{
cout << "Enter digit " << i + 1 << " of " << SIZE << ":";
cin >> A[i];
if (A[i] > 9 || A[i] < 0)
{
cout << "Enter a digit between 0 and 9" << endl;
i --;
}
cout << "\n";
}
return;
}

void inputValue2(long int B[])
{
cout << "Second array" << endl;
for(int i=0; i<SIZE; i++)
{
cout << "Enter digit " << i + 1 << " of " << SIZE << ":";
cin >> B[i];
if (B[i] > 9 || B[i] < 0)
{
cout << "Enter a digit between 0 and 9" << endl;
i --;
}
cout << "\n";
}
return;
}

void Sum(long int array1[], long int array2[])
{
string firstArrayStr;
string secondArrayStr;
long int firstNum;
long int secondNum;
long int result;
stringstream firstStream;
stringstream secondStream;

// First array
for (int counter = 0; counter < SIZE; counter++)
{
firstStream << array1[counter];
// Add this array element to string stream
firstArrayStr = firstStream.str();
}

// Second array
for (int counter = 0; counter < SIZE; counter++)
{
secondStream << array2[counter];
secondArrayStr = secondStream.str();
}

// Convert strings to integers
firstNum = atoi(firstArrayStr.c_str());
secondNum = atoi(secondArrayStr.c_str());

result = firstNum + secondNum;

cout << "result = " << result << endl;
}

```

Is This A Good Question/Topic? 0

## Replies To: Large Integer Addition - Char to Int help please!

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,349
• Joined: 08-August 08

Posted 29 November 2012 - 02:02 PM

"4321" = 1 * 10^0 + 2 * 10^1 + 3 * 10^2 + 4 * 10^3
See a pattern?

### #3 #define

• Duke of Err

Reputation: 1743
• Posts: 6,200
• Joined: 19-February 09

Posted 29 November 2012 - 02:37 PM

Characters are encode as integers - ASCII.

The decimal ASCII value of '0' is 48,
the decimal ASCII value of '1' is 49.

So '1' - '0' is 1.

```int number;
char digit = '2';

number = digit - '0';

cout << number << endl;

```

### #4 Speakeasys

Reputation: 0
• Posts: 35
• Joined: 19-September 12

Posted 03 December 2012 - 09:49 PM

I don't get it. I'm suppose to take the input from the user as type char, convert to integer. I'm using strings. How else do I do this? I can't use ASCI because what if the user enters a 20 digit number and it interferes with the ASCI input.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Posted 04 December 2012 - 05:41 AM

You can't build a number up using CTphpnwb's idea, because it would be too large at 20 digits. You can build up a number, using the method that #define has shown you.

```   char n[4]={"123"};
int num[4]={0};

len = strlen(n);
for(i=0;i<len;i++)
num[i]=n[i]-'0';

```

This post has been edited by Adak: 04 December 2012 - 05:42 AM

### #6 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,349
• Joined: 08-August 08

Posted 04 December 2012 - 06:27 AM

I think the OPs question is misstated. You can't store a 20 digit base ten integer in an int using any method. It just won't fit. You can store less than 20 digits in an unsigned long, at least on my system where an unsigned long takes 8 bytes.

If that's what the question really is, then it can be done using a variation of my method:
```int main()
{
unsigned long number = 0, incr = 9;
cout << "size of long: " << sizeof(number) << endl;
for (int i = 1; i < 20; i++) {
number += incr;
cout << number << endl;
incr *= 10;
}
return 0;
}

```

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11