i wrote a program to add bytes in array in such a way that when i add zeroth byte of two array sum should retained and carry must be added to next addition of bytes(ie array index 1 bytes)
this is program which i wrote using class
#include<iostream.h>
#define N 2
typedef unsigned char byte;
class Megaint
{
private:
byte a[N];
unsigned int R[N];
public:
Megaint();
Megaint(byte m[]);
Megaint operator+(Megaint M);
void ShowMegaint();
};
int add(byte n1,byte n2,byte *c)
{
unsigned int r,r1;
r=n1+n2+(*c);
r1=r;
r=r&0x0ff;
*c=r1>>8;
return r;
}
int add1(byte n1,byte n2,byte *c)
{
unsigned int r;
r=n1+n2+(*c);
return r;
}
Megaint::Megaint()
{
int i;
for(i=0;i<N;i++)
{
a[i]=0;
R[i]=0;
}
}
Megaint::Megaint(byte m[])
{
int i;
for(i=0;i<N;i++)
{
a[i]=m[i];
}
}
void Megaint::ShowMegaint()
{
int i;
cout<<"the sum is"<<endl;
for(i=0;i<N;i++)
{
cout<<R[i]<<endl;
}
}
Megaint Megaint::operator +(Megaint M)
{
Megaint res;
byte carry=0;
int i;
for(i=0;i<(N-1);i++)
{
res.R[i]=add(a[i],M.a[i],&carry);
}
res.R[N-1]=add1(a[N-1],M.a[N-1],&carry);
return res;
}
int main()
{
int i;
byte A[N]/*={183,128}*/,B[N]/*={183,128}*/;
cout<<"enter 1st array elements"<<endl;
for(i=0;i<N;i++)
{
cin>>A[i];........here its not taking values,it is taking only one value;
}
cout<<"enter 2nd elements"<<endl;
for(i=0;i<N;i++)
{
cin>>B[i];
}
Megaint first(A),second(B),third;
third=first+second;
third.ShowMegaint();
return 0;
}
here the problem is in main if i initialize elements into array and execute the program is working..But if try to put value in array using cin statement i could only enter one elements...You just execute this you will know what is problem...
Please help me
thanking you
sushi