I have:

void hughint::operator *=(hughint y)
{
	int i,t1,t2,prod,remainder;
	int carry=0;
	for(i=num.length()-1;i>=0;i--)
	{
		t1=(int)get(i)-(int)('0');
		cout<<"t1: "<<t1<<endl;
		t2=(int)y.get(i)-(int)('0');
		cout<<"t2: "<<t2<<endl;
		if(carry>0)
		{
		prod=(t1*t2)+carry;
		remainder%=10;
		carry=remainder/10;
		}
	}

}

I'm using 123*100 and the answer I keep getting is 123. Help please.

Recommended Answers

All 2 Replies

I have:

void hughint::operator *=(hughint y)
{
	int i,t1,t2,prod,remainder;
	int carry=0;
	for(i=num.length()-1;i>=0;i--)
	{
		t1=(int)get(i)-(int)('0');
		cout<<"t1: "<<t1<<endl;
		t2=(int)y.get(i)-(int)('0');
		cout<<"t2: "<<t2<<endl;
		if(carry>0)
		{
		prod=(t1*t2)+carry;
		remainder%=10;
		carry=remainder/10;
		}
	}

}

I'm using 123*100 and the answer I keep getting is 123. Help please.

You need to show more of your code, eg. your hughint class definition. how are you representing you ints?

I have:

void hughint::operator *=(hughint y)
{
	int i,t1,t2,prod,remainder;
	int carry=0;
	for(i=num.length()-1;i>=0;i--)
	{
		t1=(int)get(i)-(int)('0');
		cout<<"t1: "<<t1<<endl;
		t2=(int)y.get(i)-(int)('0');
		cout<<"t2: "<<t2<<endl;
		if(carry>0)
		{
		prod=(t1*t2)+carry;
		remainder%=10;
		carry=remainder/10;
		}
	}

}

I'm using 123*100 and the answer I keep getting is 123. Help please.

Your algorithm does nothing to the internal representation of your integer which I am assuming is a string due to the .length() call. Although you work out various products etc. you are not storing them anywhere, hence when you print your string at the end it is unchanged.

Think about doing something like...

void hughint::operator*=( const hughint& y )
{
   string tempint = localintstring;
   // do your calculations by extracting each digit of the string 
   // as you currently are but this time usign tempint as the source
   // store the results in localintstring
   t1=(int)get(i)-(int)('0'); // rather that get(i) extract the char at 
                                       // offset i from tempint and use it in the
                                       // calculation instead.
   cout<<"t1: "<<t1<<endl;
   t2=(int)y.get(i)-(int)('0');
   cout<<"t2: "<<t2<<endl;
   if(carry>0)
   {
      // having worked these values out what are your doing with
      // them?  nothing, you need to determine where to store them
      prod=(t1*t2)+carry;
      remainder%=10;
      carry=remainder/10;
   } 
}

Also I suspect you have some issues with the algorithm chosen, although I am not going to correct those for 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.