C++/c++

Advertisement


Question
Hi Prince M.Premnath,
I'm new in c++ and I've writen a program which can produce ascii codes.Could you write 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!
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
Hai dear Tina !


char data type have some limitations it cannot go beyond 0-255 once if it reaches 255 limit further any increment will reset the value to 0 this will end up in an infinite loop,

please find the modified code below , i suggest you to go with this

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

  cout <<"*************** ASCII Table **************** "<<endl ;
  cout<<endl;
    
   for (int uch=32; uch<=255; uch++)
  {
        cout<<right;
        cout<<setw(5)<<uch<<setw(5)<<char(uch);
  }
  cin.get();
  return 0;
}


Get back to me in case of queries :)

Thanks and Regards !

Prince M. Premnath

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

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