Hi. I'm having trouble with this overloaded >> function.
Code:
//Uses iostream.h, ctype.h, stdlib.h, and constants TRUE and FALSE:
istream& operator >>(istream& ins, Money& amount)
{
char one_char, decimal_point,
digit1, digit2; //digits for the amount of cents
long dollars;
int cents,
negative;//set to TRUE if input is negative.
ins >> one_char;
if (one_char == '-')
{
negative = TRUE;
ins >> one_char; //read '$'
}
else
negative = FALSE;
//if input is legal, then one_char == '$'
ins >> dollars >> decimal_point >> digit1 >> digit2;
if ( one_char != '$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2) )
{
cout << "first";
cin.get();
cin.get(one_char);
cout << one_char;
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cout << "last";
cin.get();
cout << "Error illegal form for money input. Try again\n";
system("PAUSE");
exit(1);
}
cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
amount.all_cents = dollars*100 + cents;
if (negative)
amount.all_cents = -amount.all_cents;
return ins;
}
This is code from my book(without the cin.gets of course), and my program compiles fine. I'm was trying to modify it so that the user can reenter input if he messes up, instead of using exit(1). I think i got it working for some input, but If i input like '1' and then 'a' or 'a' and then 'd'...basicly, if i enter a character as my second input, which i believe goes into the variable dollars, the program doesn't let me input the rest of the variables, skips every cin.get(), and goes straight for the exit(1). Why? Sorry if this is easy, but I can't figure it out. I know 'dollars' is a 'long int' variable, but my book does say you can store a letter in an int. (I don't want to, but this behavior is prevented from allowing the user to reenter inputs sometime.)
Thanks. :\