// // Example 8.5. This interactive program repeatedly asks // the user for a character and prints its ASCII code. // #include const char YES = 'y'; const char NO = 'n'; char GetCharNewline(void); int main(void) { char GetAnswer(void); char in_char, // input character to encode answer; // answer to continue? cout << "Given a character, this program prints its ASCII code.\n"; do { cout << "Enter a character: "; in_char = GetCharNewline(); cout << "The ASCII value of " << in_char << " is " << int(in_char) << ".\n"; answer = GetAnswer(); } while (answer == YES); return 0; } // // Function to read and return a character and to move the // input buffer pointer past the newline character // Pre: none // Post: An input character was returned and the buffer flushed. // char GetCharNewline(void) { char in_char; cin.get(in_char); if (in_char != '\n') cin.ignore(256, '\n'); return in_char; } // // Function to ask if user wishes to continue and to return // the user's response after flushing the input buffer // Pre: none // Post: A y or an n was returned and the buffer flushed. // char GetAnswer(void) { char answer; // answer for wanting another code do { cout << "\nDo you want to find another ASCII code? (y/n) "; answer = GetCharNewline(); } while ( !(answer == YES || answer == NO) ); return answer; }