#include #include #include #include "myio.h" using namespace std; //************************************************************************** // function to clear the cin stream up to & including the next new line // // Post-condition -- all data up to and including the next new line char // is removed from cin //************************************************************************** void clrStream() { while (cin.peek() != '\n') cin.ignore(); cin.ignore(); } //************************************************************************** // function to get a character response from the user and use the default // response if the user just hits the return key // // parameter usage // defalt -- imports the default character to use if a new line is pressed // // Post-condition -- the character returned is defalt if the next character // in the cin stream is a new line, otherwise it is the // character entered and the stream is cleared //************************************************************************** char getResponse(char defalt) { char ch = cin.get(); if (ch == '\n') ch = defalt; // use the default character else clrStream(); // clear the CR and any garbage entered return ch; } //************************************************************************** // function to obtain a valid integer from the user // // parameter usage // i -- exports the integer obtained // // Post-condition -- i will be a valid integer //************************************************************************** void getValid(int& i) { while (true) { cin >> i; if (cin.good()) break; // valid int extracted so exit if (cin.eof()) break; // end of file so outta here cout << "Invalid integer entered!! Reenter -- "; cin.clear(); // clear the error flag clrStream(); // clear bad data from the stream } } //************************************************************************** // function to obtain a valid real number from the user // // parameter usage // d -- exports the real number obtained // // Post-condition -- d will be a valid real number //************************************************************************** void getValid(double& d) { while (true) { cin >> d; if (cin.good()) break; // valid real number extracted so exit if (cin.eof()) break; // end of file so outta here cout << "Invalid real entered!! Reenter -- "; cin.clear(); // clear the error flag clrStream(); // clear bad data from the stream } } //************************************************************************** // function to attach a file to an input stream // // parameter usage // iStr -- exports the stream the file is attached to // // Post-condition -- iStr is attached to a valid file, ready for extraction //************************************************************************** void openIn(ifstream& iStr) { string filename; cout << "Enter filename -- "; while (true) { cin >> filename; iStr.open(filename.c_str()); if (iStr) break; // succesfully attached the file so outta here // unable to open file clrStream(); // so clear any left over chars from cin stream cout << "Unable to open file!!! " << filename // and complain << ". Re-enter filename -- " << endl; } clrStream(); // got a valid name, now clear any leftover characters } //************************************************************************** // function to attach a file to an output stream // // parameter usage // oStr -- exports the stream the file is attached to // // Post-condition -- oStr is attached to a valid file, ready for insertion //************************************************************************** void openOut(ofstream& oStr) { string filename; cout << "Enter filename -- "; while (true) { cin >> filename; oStr.open(filename.c_str()); if (oStr) break; // succesfully attached the file so outta here // unable to open file clrStream(); // so clear any left over chars from cin stream cout << "Unable to open file!!! " << filename // and complain << ". Re-enter filename -- " << endl; } clrStream(); // got a valid name, now clear any leftover characters }