Hi all... I am a new poster to the site but have lurked in here several times to sort out some sticky issues in the past... You guys & gals are great and I hope to be able to get to your level of programming knowledge at some point!
I am currently taking a C++ OOP course and have run across an interesting problem. Unfortunately, I am an online student and the instructor is generally not much help.
I was having fits with the failbit situation to check for valid inputs. I have already found a thread that provided some excellent guidance to get me in the ballpark here . My thanks to those posters for the assistance.
I have written some code based on several different examples in our Deitel text using stream extraction and insertion and have run across an interesting problem...
The program does what I need it to with one exception... The first input sets the failbit if either a character or non-int is entered (perfect!). The second input, however, only sets the failbit if a character is entered... If you enter a decimal as the second (y) value, the failbit does not set...Code:#include <iostream> using std::cin; using std::cout; using std::endl; using std::istream; using std::ostream; #include <limits> // Class Point class Point { friend ostream &operator<<( ostream&, const Point & ); friend istream &operator>>( istream&, Point & ); private: int xCoordinate; // X coordinate int yCoordinate; // Y coordinate }; //end class Point // overloaded stream-extraction operator istream &operator>>( istream &input, Point &num ) { input >> num.xCoordinate; input >> num.yCoordinate; return input; } // end function operator>> // overloaded stream-insertion operator; ostream &operator<<( ostream &output, const Point &num ) { if (!(cin.fail())) output << "The coordinates you entered were: " << '[' << num.xCoordinate << ", " << num.yCoordinate << ']'; else { cin.clear(); cin.ignore(5000,'\n'); output << "Invalid data entered." << endl; } return output; } // end function operator<< // main point test program int main() { // Create Point objects for program Point point1; Point point2; Point point3; cout << "Please enter 2 integers and press Enter:\n"; cin >> point1; cout << point1 << endl; cout << "\n\nPlease enter a character followed by an integer and press Enter:\n"; cin >> point2; cout << point2 << endl; cout << "\n\nPlease enter an integer followed by a character and press Enter:\n"; cin >> point3; cout << point3 << endl; return 0; } // end main
I added a "cin.fail()" on the output side to confirm this. Why would failbit set for a character and not a non-int on the second value only? I have looked everywhere on the Web I can think of and could not find anything that explained it.
Another interesting side note is that I noticed that when you enter an invalid value of any kind for the first value, the program goes right to my "invalid" message without waiting for my second input. As my code reads, I would think that the program would wait for two inputs before it performs the ostream failbit check I have added. This problem is not a dealbreaker for my assignment since it does stop the invalid input, but I was curious because I really want to learn and understand the concepts for my benefit.
Any assistance provided would be greatly appreciated.



LinkBack URL
About LinkBacks


