SOURCE FILE: ex0809.cpp


// Test program for OurToUpper in Example 8.9
   
#include <iostream.h>
#include <ctype.h>   
  
const int YES = 'y';
const int NO  = 'n';
 
int main(void)   
{   
   char OurToUpper(char);
   char GetCharNewline(void);    
   char GetAnswer(void);  

   char in_char,    // input character to encode 
        answer;     // answer to continue? 
      
   cout << "Given a letter, this program prints in uppercase.\n";
   do   
   {   
      cout << "Enter a character: ";
      in_char = GetCharNewline();   
         
      cout << "The uppercase equivalent is " 
           << OurToUpper(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 print another character? (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') || ('z' < ch))   
      c = ch;   
   else   
      c = char(ch - 'a' + 'A');   
   
   return c;   
}