Tuesday, December 6, 2016

Programming Challenge 5.2 - ASCII Codes

/* ASCII Codes - This program uses a loop to display the characters for the ASCII
   codes 0 through 127. 16 characters are displayed per line. */

#include "Utility.h"

int main()
{
    /* Constant: Min char, Max char */
    const int MIN_CHAR = 0,
              MAX_CHAR = 127;

    /* For loop: charASCII is defined and initialized in the loop header, since
       it is only used inside the loop */
    for (int charASCII = MIN_CHAR; charASCII <= MAX_CHAR; charASCII++)
    {
        /* charASCII is converted by typecasting int to char, and
           the characters from 0 through 127 are displayed */
        cout << charASCII << " " << static_cast<char>(charASCII) << " |";

        /* Check: If a value is divisible by 16 and the remainder is 0,
           the linebreak will occur */
        if ((charASCII % 16) == 0)
        {
            cout << endl;
        }
    }
    cout << endl;

    pauseSystem();
    return 0;
}

No comments:

Post a Comment