too many numbers that i dont want

i dont know how to fix the problem.

Page 1 of 1

2 Replies - 445 Views - Last Post: 07 November 2009 - 06:46 PM Rate Topic: -----

#1 ninreznorgirl2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-April 09

too many numbers that i dont want

Posted 07 November 2009 - 05:06 PM

i am overloading the assignment operator. the problem i am having is that it is printing more numbers then what it is supposed to. i know what the problem is, but i dont know how to fix it. the k is incremented in the beginning, and so its printing the extra numbers that k has stored in it. any ideas about how to go around this problem?? it is supposed to be printing 0.123 if that is the number entered but its printing 0.-52 -52123

help??

 MyFloat& MyFloat::operator= (const char RightSide[])
{
	int k = 0;
	
	NumberOfDigits = 0;	  

	if ((RightSide[k] != '0' && RightSide[k] != '.'))
		return *this;

	while( RightSide[k] == '0' ||  isspace(RightSide[k]))
		k++;

	if (RightSide[k] != '.')
		return *this;	
	k++;

	while(isdigit(RightSide[k]) && k < RightSide[k])   
	{									   
		 Number[k] = RightSide[k] - '0';		
		 k++;
	}

	NumberOfDigits = k;

	for(k; k < MAX_DIGITS; k++)	//insert an end buffer of zeros
		Number[k] = 0;

	return *this;
}



this is where the overloaded operator is being tested:
void TestAssignment()
{
  MyFloat X;
  char	X_Str[100];

  cout << "\n------------  Testing \"=\" for MyFloat --------------------\n";
  do
	{
	cout << "\nEnter string holding float: ";
  
	cin.getline(X_Str, 100);

	X = X_Str;  // Call MyFloat = operator

	if ( X.Digits() == 0 )	   //  Error in string format
	  cout << "\nFormat error! ";

	cout << "\nAfter assignment,	 'X = "<< X << "'" << endl;
	}
  while ( SpaceBarToContinue() );
}



Is This A Good Question/Topic? 0
  • +

Replies To: too many numbers that i dont want

#2 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: too many numbers that i dont want

Posted 07 November 2009 - 06:37 PM

MyFloat& MyFloat::operator= (const char RightSide[]) 
// what is the equal sign for, its a function, not a equation


I could be wrong.

This post has been edited by redhotfire0: 07 November 2009 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 ninreznorgirl2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-April 09

Re: too many numbers that i dont want

Posted 07 November 2009 - 06:46 PM

i'm overloading the assignment operator
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1