//Demonstrates the function read_and_clean. #include #include #include void read_and_clean(int& n); //Reads a line of input. Discards all symbols except the digits. Converts //the digit string to an integer and sets n equal to the value of this integer. void new_line( ); //Discards all the input remaining on the current input line. //Also discards the '\n' at the end of the line. int main( ) { int n; char ans; do { cout << "Enter an integer and press return: "; read_and_clean(n); cout << "That string converts to the integer " << n << endl; cout << "Again? (yes/no): "; cin >> ans; new_line( ); } while ( (ans != 'n') && (ans != 'N') ); return 0; } //Uses iostream.h, stdlib.h, and ctype.h: void read_and_clean(int& n) { const int ARRAY_SIZE = 6; char digit_string[ARRAY_SIZE]; char next; cin.get(next); int index = 0; while (next != '\n') { if ( (isdigit(next)) && (index < ARRAY_SIZE - 1) ) { digit_string[index] = next; index++; } cin.get(next); } digit_string[index] = '\0'; n = atoi(digit_string); } //Uses iostream.h: void new_line( ) { char symbol; do { cin.get(symbol); } while (symbol != '\n'); }