944,089 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 463
  • C++ RSS
Nov 6th, 2009
0

overloading assignment operator problem

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Nov 6th, 2009
-7
Re: overloading assignment operator problem
lines 8 and 11 of the first code are using uninitialized variable ch
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Win32 API and C++
Next Thread in C++ Forum Timeline: Input time as hh:mm and no other way





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC