Printing a Table of ASCII Values using C++ programming
Write a program that uses a for statement to print a table of ASCII values for the characters in the ASCII character set from 33 to 126. The program should print the decimal value, octal value, hexadecimal value and character value for each character.
Use the stream manipulators dec, oct and hex to print the integer values.
Answer:
#include <iostream> #include <iomanip> using namespace std; int main() { // display column headings and set field lengths cout << setw( 7 ) << "Decimal" << setw( 9 ) << "Octal " << setw( 15 ) << "Hexadecimal " << setw( 13 ) << "Character" << showbase << '\n'; // loop through ASCII values 33-126 and display corresponding // integer, octal and hexadecimal values for ( int loop = 33; loop <= 126; ++loop ) cout << setw( 7 ) << dec << loop << setw( 9 ) << oct << loop << setw( 15 ) << hex << loop << setw(13) << static_cast< char >( loop ) << endl; }
Leave a reply