// Test program for ourToUpper in Example 8.9 #include #include using namespace std; const char YES = 'y'; const char NO = 'n'; int main() { char ourToUpper(char); // prototypes char getCharNewline(); char getAnswer(); char inChar; // input character to encode char answer; // answer to continue? cout << "This program prints the uppercase equivalent of a lowercase character." << endl; do { cout << "Enter a character: "; inChar = getCharNewline(); cout << "The uppercase equivalent is " << ourToUpper(inChar) << "." << endl; 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() { char inChar; cin.get(inChar); if (inChar != '\n') cin.ignore(256, '\n'); return inChar; } // // 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() { char answer; // answer for wanting another code do { cout << endl << "Do you want to find another ASCII code? (y/n) "; answer = getCharNewline(); answer = tolower(answer); } while ( !(answer == YES || answer == NO) ); return answer; } // // Example 8.9. Function to return uppercase equivalent of // lowercase letter ch. If ch is not a lowercase letter, // return ch. // Pre: ch is a character. // Post: The uppercase equivalent of ch was returned. // If ch was not a lowercase letter, ch was returned. // char ourToUpper(char ch) { char c; if ((ch < 'a') || (ch > 'z')) c = ch; else c = char(ch - 'a' + 'A'); return c; }