Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

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;
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!