I am having difficulty in using array to read large unsigned integer and write them and calculate their sum.Also i am going to implement the following code to check digit and character.

for (;;)
    {
        ch = cin.get();

        if (!isdigit(ch)) break; 
        cout << ch;
    }

 and 



    for (;;)
    {
        ch = cin.peek();

        if (!isdigit(ch)) break;
                 cin.get();
        cout << ch;
    }

And ,To skip leading whitespace characters, i am using iswhitespace() function as
while (iswhitespace(cin.peek()))
cin.get();

Any suggestion would be very helpful to me.Although it seems easier to use array but i am unable to handle large unsigned integers to read ,store and add them.

i don't understand why are you checking characters, but it is ok...

what is the problem adding the numbers?

Because the code must read the digit one by one as individual characters(CHAR values)and convert them to numeric form.And you know C++ uses ASCII code for a character when it appears in an arithmetic expression.
Also how do u handle for the carry from previous sum?
I am coding for three functions ReadLargeInteger,WriteLargeInteger,AddLargeInteger.
any suggestion,

i guess the answer to your question is passing the parameters by value...

post your code and it shall be easier...

> Because the code must read the digit one by one as individual characters(CHAR values)and convert them to numeric form.
or you can read the literal digits into a string; this would be easier.

> And you know C++ uses ASCII code for a character when it appears in an arithmetic expression.
not necessarily true for C++ or even C. this is all that you can assume:

The code value zero is reserved for the null character which is always in the target character set. Code values for the basic C character set are positive when stored in an object of type char. Code values for the digits are contiguous, with increasing value. For example, '0' + 5 equals '5'. Code values for any two letters are not necessarily contiguous.

> Also how do u handle for the carry from previous sum?
simulate adding integers by hand. here is an example (perhaps not suitable for submission as a homework assignment)

#include <string>
#include <vector>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <cassert>
#include <boost/lambda/lambda.hpp>
#include <boost/operators.hpp>
using namespace std ;
using namespace boost ;
using namespace boost::lambda ;

template < size_t NDIGITS > 
struct integer : private addable< integer<NDIGITS> > 
{
  integer() : digits(NDIGITS) {}
  explicit integer( const string& str ) : digits(NDIGITS)
  {
    assert( str.size() <= NDIGITS ) ;
    for( size_t i=0 ; i<str.size() ; ++i ) { assert( isdigit(str[i]) ) ; }
    transform( str.begin(), str.end(),
               digits.begin() + NDIGITS - str.size(),
                _1 - '0' ) ;
  }
  integer<NDIGITS>& operator+= ( const integer<NDIGITS>& that )
  {
    int carry = 0 ;
    for( int i = NDIGITS-1 ; i>=0 ; --i )
    {
      int temp = digits[i] + that.digits[i] + carry ;
      digits[i] = temp % 10 ;
      carry = temp / 10 ;
    }
    assert( carry == 0 ) ; // overflow has not occurred
    return *this ;
  }
  vector<int> digits ;
};

template< typename STREAM, size_t NDIGITS >
inline STREAM& operator<< ( STREAM& stm, const integer<NDIGITS>& i )
{
  for_each( find_if( i.digits.begin(), i.digits.end(), _1 != 0 ),
                                 i.digits.end(), stm << _1 ) ;
  return stm ;
}

int main()
{
  integer<100> i("123456789123456789123456789"), j("999999875699999") ;
  cout << i << " + " << j << " == " << i + j << '\n' ;
}
/**
>g++ -std=c++98 -Wall -I/usr/local/include big_int.cpp && ./a.out
123456789123456789123456789 + 999999875699999 == 123456789124456788999156788
*/

Anyway thanks for the code but right now i am not familiar about vectors.so i just know about Array and i want to do this with Array.BTW i will look up on code and try to learn vector too..

I wouldn't bother looking at that code too closely since you'll need the boost libraries for it to work. But you don't need them for your purpose, in fact it is much of an overkill.


What you should do, is plan how you would do this on paper. Once that is clear in your mind it is plain sailing.

Could you pls.give me information about that addition mechanism in mathematics to carry out manually in paper. or any mathematics sites also works..

It's a sorry state of affairs if you don't know how to do addition on paper.

char a[30], b[30], c[30];

   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
a: | | | | |1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
b: | | | | | | | | | |9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|6|7|8|9|
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
c: | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

I would suggest you begin by printing this out and completing the calculation by hand, and study very carefully the operations you're performing.

This article has been dead for over six months. Start a new discussion instead.