//************************************************************************** // function to get a character response from the user and use the default // response if the user just hits the return key // /// \param [in] defalt imports the default character to use if the enter /// key is pressed /// \returns defalt if the next character in the cin stream is a new line, /// otherwise it returns the character entered /// \post the cin stream is cleared //************************************************************************** char getResponse(char defalt) { char ch = cin.get(); if (ch == '\n') ch = defalt; // use the default character else clrStream(); // clear the newline and any garbage entered return ch; } //************************************************************************** // function to get a valid int -- pre-test version // // parameter usage -- i exports the integer obtained // // post-condition -- i is a valid integer and the cin stream marker is left // at the first character after the extracted integer //************************************************************************** void getValid(int& i) { cin >> i; while (!(cin || cin.eof())) { cout << "Invalid integer entered!! Reenter -- "; cin.clear(); // clear the error flag clrStream(); // clear bad data from the stream cin >> i; } } //************************************************************************** // function to get a valid int -- post-test version -- requires a flag!!! // // parameter usage -- i exports the integer obtained // // post-condition -- i is a valid integer and the cin stream marker is left // at the first character after the extracted integer //************************************************************************** void getValid(int& i) { bool ok; do { ok = true; if (!(cin>>i) && !cin.eof()) { cout << "Invalid integer entered!! Reenter -- "; cin.clear(); // clear the error flag clrStream(); // clear bad data from the stream ok = false; // set our flag to stay in loop } } while (!ok); } //************************************************************************** // function to obtain a valid integer from the user -- infinite loop version // // parameter usage -- i exports the integer obtained //************************************************************************** void getValid(int& i) { while (true) { cin >> i; if (cin || cin.eof()) break; // valid int or eof so exit cout << "Invalid integer entered!! Reenter -- "; cin.clear(); // clear the error flag clrStream(); // clear bad data from the stream } } //************************************************************************** // function to get a valid int -- testing the operator // // parameter usage -- i exports the integer obtained // // post-condition -- i is a valid integer and the cin stream marker is left // at the first character after the extracted integer //************************************************************************** void getValid(int& i) { while (!(cin>>i || cin.eof())) { cout << "Invalid integer entered!! Reenter -- "; cin.clear(); // clear the error flag clrStream(); // clear bad data from the stream } }