| | |
overloading assignment operator problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
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.
here is the code that is used to test the overloaded function:
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?
C++ Syntax (Toggle Plain Text)
MyFloat& MyFloat::operator= (const char RightSide[]) { char ch; int k = 0; NumberOfDigits = 0; if ( !cin.good() || (ch != '0' && ch != '.')) return *this; while( ch == '0' || isspace(ch)) cin.get(ch); if (ch != '.') return *this; cin.get(ch); while(isdigit(ch) && k < RightSide[k]) { Number[k] = ch - '0'; k++; cin.get(ch); } NumberOfDigits = k; cin.putback(ch); for(k; k < MAX_DIGITS; k++) //insert an end buffer of zeros Number[k] = 0; return *this; }
here is the code that is used to test the overloaded function:
C++ Syntax (Toggle Plain Text)
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() ); }
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?
![]() |
Similar Threads
- Unclear on what to do with this overloading assignment (C++)
- Does it need a copy constructor and an operator= for a class which have array member (C++)
- Classes and assignment operator (C++)
- about operator overloading (C++)
- Overloading assignment operator (C++)
Other Threads in the C++ Forum
- Previous Thread: Win32 API and C++
- Next Thread: Input time as hh:mm and no other way
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






