C++/ascii

Advertisement


Question
Hi Vijayan,
I'm new in c++ and I've writen a program which can produce ascii codes.Could you help me how can I write this program that when I run it,it show me ascii codes columnar?
 32  64  96 128  160  192  224
 33  65  97 129  161  193  225
and so on...
I tried do this with two 'for' but it doesn't work.I cannot use
char 255! an extended beep!!
here is program :
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

cout <<"*************** ASCII Table **************** "<<endl ;
cout<<endl;
     
      
          for(int col=1; col<=7; col++)
          {
          for(int row=1; row<=32; row++)
                  {
            for (unsigned char uch=32; uch<=254; uch++)
               {
           cout<<right;
           cout<<setw(5)<<uch<<setw(5)<<int(uch);
               }
          }
             }
       //cout<<endl;
           
return 0;
}


Regards,
Tina.

Answer
> I tried do this with two 'for' but it doesn't work.

Of course it will work if you set up the loops correctly.


> I cannot use char 255

ASCII reserves the first 32 codes (numbers 0–31 decimal) for control characters.
Code 32 is the invisible space character.
Codes from 33 to 126 are the printable characters, representing letters, digits, punctuation marks, and a few miscellaneous symbols.
The remaining character codes are not printable.

See: http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters


#include <iostream>
#include <iomanip>

int main()
{
 enum { SPACE = 32, INCREMENT = 32, MAXCHAR = 128 } ;

 for( int i = 0 ; i < INCREMENT ; ++i )
 {
    for( int j = SPACE ; j < MAXCHAR ; j += INCREMENT )
    {
       char ch = i+j ;
       std::cout << std::setw(6) << char(ch)
                 << std::setw(4) << int(ch) ;
    }
    std::cout << '\n' ;
 }
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.