6 Replies - 492 Views - Last Post: 04 December 2012 - 07:12 AM Rate Topic: -----

#1 Speakeasys  Icon User is offline

  • New D.I.C Head

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

Large Integer Addition - Char to Int help please!

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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: Large Integer Addition - Char to Int help please!

Posted 29 November 2012 - 02:02 PM

"4321" = 1 * 10^0 + 2 * 10^1 + 3 * 10^2 + 4 * 10^3
See a pattern?
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: Large Integer Addition - Char to Int help please!

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;


Was This Post Helpful? 1
  • +
  • -

#4 Speakeasys  Icon User is offline

  • New D.I.C Head

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

Re: Large Integer Addition - Char to Int help please!

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.
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Large Integer Addition - Char to Int help please!

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

Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: Large Integer Addition - Char to Int help please!

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


Was This Post Helpful? 0
  • +
  • -

#7 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Large Integer Addition - Char to Int help please!

Posted 04 December 2012 - 07:12 AM

Correct - he needed a computer representation of an integer, not a mathematical integer, which your method would provide.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1