// // Example 8.8. This interactive program repeatedly asks the user for a // character and prints its ASCII code. // #include #include const int YES = 'y'; const int 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: y for input of y or Y, n for n or N // 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(); answer = tolower(answer); } while (!(answer == YES || answer == NO)); return answer; }