overloading assignment operator problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 11
Reputation: ninreznorgirl2 is an unknown quantity at this point 
Solved Threads: 0
ninreznorgirl2 ninreznorgirl2 is offline Offline
Newbie Poster

overloading assignment operator problem

 
0
  #1
Nov 6th, 2009
here is the overloaded function i have. I have to enter a float, for example, .123 or 0.123. if there is many zeros in front, then it will skip them over, and test for a decimal, then count the numbers in the end. and then print them out.

  1. MyFloat& MyFloat::operator= (const char RightSide[])
  2. {
  3. char ch;
  4. int k = 0;
  5.  
  6. NumberOfDigits = 0;
  7.  
  8. if ( !cin.good() || (ch != '0' && ch != '.'))
  9. return *this;
  10.  
  11. while( ch == '0' || isspace(ch))
  12. cin.get(ch);
  13.  
  14. if (ch != '.')
  15. return *this;
  16.  
  17. cin.get(ch);
  18.  
  19. while(isdigit(ch) && k < RightSide[k])
  20. {
  21. Number[k] = ch - '0';
  22. k++;
  23. cin.get(ch);
  24. }
  25.  
  26. NumberOfDigits = k;
  27. cin.putback(ch);
  28.  
  29. for(k; k < MAX_DIGITS; k++) //insert an end buffer of zeros
  30. Number[k] = 0;
  31.  
  32. return *this;
  33. }

here is the code that is used to test the overloaded function:

  1. void TestAssignment()
  2. {
  3. MyFloat X;
  4. char X_Str[100];
  5.  
  6. cout << "\n------------ Testing \"=\" for MyFloat --------------------\n";
  7. do
  8. {
  9. cout << "\nEnter string holding float: ";
  10.  
  11. cin.getline(X_Str, 100);
  12.  
  13. X = X_Str; // Call MyFloat = operator
  14.  
  15. if ( X.Digits() == 0 ) // Error in string format
  16. cout << "\nFormat error! ";
  17.  
  18. cout << "\nAfter assignment, 'X = "<< X << "'" << endl;
  19. }
  20. while ( SpaceBarToContinue() );
  21. }

the problem i am having with this is that it is going right into the if statement if i initialize ch with zero. but then if i put cin.get(ch) between the NumberOfDigits and the if statement, it works, but since there is the cin.getline in the testing function, it asks for a number twice, and i dont want it to do that. help?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,561
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1484
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #2
Nov 6th, 2009
lines 8 and 11 of the first code are using uninitialized variable ch
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC