Hey,hoping someone can help me. I'm having an error in my project. This is the problem for the project:

In C++, the largest int value is 2, 147, 483, 647. So, an integer larger than this cannot be
stored and processed as an integer. Similarly, if the sum or product of two positive integers
is greater than 2, 147, 483, 647, the result will be incorrect. One way to store and manipulate
large integers is to store each individual digit of the number in an array.
Design a class named largeIntegers such that an object of this class can store an
integers of, at most, 20 digits. Your program should inputs two positive integers of, at most,
20 digits. Then add operations to add, subtract, multiply, and compare integers stored in two
objects. Also add constructors to properly initialize objects and functions to set, retrieve,
and print the values of objects. If the result of the numbers has more than 20 digits, output
the result with an appropriate message.
Add a main function to test all of your class members. (Hint: Read numbers as strings
and store the digits of the number in an array.)

I get an error saying: error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

This is error is pointing to void largeintegers::Input()

This is my code:

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

#define DIGITS 20
class largeintegers
{
public:
largeintegers();
void Input();
void Output();
largeintegers operator+( largeintegers );
largeintegers operator-( largeintegers );
largeintegers operator*( largeintegers );
int operator==( largeintegers);
private:
int integer[ DIGITS ];
int len;
};
void largeintegers::Output()
{
int i;
for (i=len-1;i >= 0;  i-- )
cout<<integer[i];
}
void largeintegers::Input()             Here is where the error is
{
    string in;
    int i,j,k;
    cout << "Enter a number("<<DIGITS<<" digits max):";
    cin >> in;                                           This is where it points to the error!
    for(i=0;in[i]!='\0';i++);
    len=i;
    k=0;
    for(j=i-1;j>=0;j--)
    integer[j]=in[k++]-48;
}
largeintegers::largeintegers( )
{
for ( int i = 0; i <DIGITS; i++ )
      integer[ i ] = 0;
len=DIGITS-1;
}
int largeintegers::operator==( largeintegers op2 )
{int i;
if(len<op2.len)
      return -1;
 if(op2.len<len)
      return 1;
 for(i=len-1;i>=0;i--)
      if(integer[i]<op2.integer[i])
           return -1;
       else if(op2.integer[i]<integer[i])
             return 1;
  return 0;
}
largeintegers largeintegers::operator+( largeintegers op2 )
{largeintegers temp;
int carry = 0;
int c,i;
if(len>op2.len)
    c=len;
else
    c=op2.len;


for ( i=0; i<c; i++ )
     {temp.integer[ i ] =integer[ i ] + op2.integer[ i ] + carry;
     if ( temp.integer[ i ] > 9 )
         {temp.integer[ i ] %= 10;
          carry = 1;
         }
     else
          carry = 0;
     }
if(carry==1)
    {temp.len=c+1;
    if(temp.len>=DIGITS)
          cout<<"***OVERFLOW*****\n";
    else
         temp.integer[i]=carry;

    }
else    
   temp.len=c;
return temp;
}
largeintegers largeintegers::operator-( largeintegers op2 )
{largeintegers temp;
int c;
 if(len>op2.len)
    c=len;
else
    c=op2.len;
int borrow = 0;
for( int i = c;i >= 0;i--)
if(borrow==0)
    {if(integer[i]>=op2.integer[i])
           temp.integer[i]=integer[i]-op2.integer[i];
    else
          {borrow=1;
          temp.integer[i]=integer[i]+10-op2.integer[i];
          }
    }
else
    {borrow=0;
    if(integer[i]-1>=op2.integer[i])
           temp.integer[i]=integer[i]-1-op2.integer[i];
      else
          {borrow=1;
          temp.integer[i]=integer[i]-1+10-op2.integer[i];
          }
      }
temp.len=c;
return temp;
}   
largeintegers largeintegers::operator*(  largeintegers op2 )
    { largeintegers temp;
      int i,j,k,tmp,m=0;
       for (int i=0; i<op2.len; i++)
        { k=i;
          for (j=0; j< len; j++)
           {tmp = integer[ j ] * op2.integer[i];
            temp.integer[k] =temp.integer[k]+tmp;
            temp.integer[k+1] =temp.integer[k+1]+ temp.integer[k]/10;
            temp.integer[k] %=10;
            k++;
            if(k>m)
                m=k;
                }
        }
        temp.len=m;
         if(temp.len>DIGITS)
          cout<<"***OVERFLOW*****\n";
          return temp;
    }


using namespace std;
int main()
{int c;
largeintegers n1,n2,result;
n1.Input();
n2.Input();
n1.Output();
cout <<" + ";
n2.Output();
result=n1+n2;
cout<< " = " ;
result.Output();
cout << "\n\n";
n1.Output();
cout <<" - ";
n2.Output();
result=n1-n2;
cout<< " = " ;
result.Output();
cout << "\n\n";
n1.Output();
cout <<" * ";
n2.Output();
result=n1*n2;
cout<< " = " ;
result.Output();
cout << "\n\n";
c=n1==n2;
n1.Output();
switch (c)
  {case -1: cout<<" is less than ";
            break;
   case 0: cout<<" is equal to ";
            break;
    case 1: cout<<" is greater than ";
            break;
    }  
n2.Output();
 cout << "\n\n";                
system("pause");
}

Any help with this error would be appreciated, my project is due by sunday night. I spent
so much time pulling my hair off not knowing why this error.

Recommended Answers

All 2 Replies

This code builds fine for me. Which compiler/platform are you using?

There are a couple of changes that I'd make though. For example, You don't need to do find the length of the number in the way that you do on line 32. In fact, I don't think that there's anything that says that a std::string needs to be null-terminated, so this approach is unportable at best, but maybe just plain wrong. If you need to do this, you can simply do:

len = in.length();

to get the length.

What you probably should do at this point is check that the thing that is entered is a valid number. For example, I think your code would accept "Hello!" as a number without complaining. You can use the isdigit function in cstdlib for this, or just manually check:

char c;
std::cin >> c;
if ( c >= '0' && c <= '9' )
    std::cout << "Yay, a digit!";
else
    std::cout << "That's not a digit!";

Notice how there is an assumption that the characters 0 to 9 run sequentially and contiguously in their character representation. As far as I know, this is pretty safe for any representation that you are likely to be using. You also have a hard-coded value of 48 on line 36 for the value of 0. I would change this line to:

integer[ j ] = in[ k++ ] - '0';

It's clearer to the reader what's going on.

The other thing that I'd do is use unsigned char to represent each digit, since you're never going to need a number bigger than 9, it seems like a bit of a waste of space to use a whole int for each digit!

Have fun!

hi thanks! I'm using visual c++ 2010 express. Okay I will try what you advised, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.