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.
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:
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?