for loop and ASCII table

I'm new to programming and I would like some assistance on a little project I am doing for my class. I'm not asking for anyone to do the work for me but I just need to know if I have the right idea at this point.

Using a for loop I want to print the entire ASCII table and I'm having trouble with the non printing characters. I'm trying figure out a way decide the console output for these characters: \n, \t, \a, etc...This is what I have tried so far;

int main (){
int n, i, lim;

cout << "\nHow many characters would you like to print per line? [1, 4]: ";
cin >> lim; cin.ignore(80,'\n');

while(lim < 1 || lim > 4){
cout << "Please input a number from 1 to 4: ";
cin >> lim; cin.ignore(80,'\n');
}

for (i = 0; i <= 255; i++)
cout << i << " " << (char) i << " " << "||" << (i % lim == 0 ? '\n' : '\t');

if (i == 0)
cout << i << " " << "NULL" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

else if (i == 7)
cout << i << " " << "\a" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

else if (i == 8)
cout << i << " " << "\b" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

else if (i == 9)
cout << i << " " << "\t" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

else if (i == 10)
cout << i << " " << "\n" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

else if (i == 11)
cout << i << " " << "\v" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

else if (i == 13)
cout << i << " " << "\r" << " " << "||" << (i % lim == 0 ? '\n' : '\t');


system("pause");
return 0;
}
[code] Your code goes here [/code]
If you want to show "\n" you will need to escape the \ character "\\n"
Also put that if else in a block so it can be executed in the for (a switch may look better)
1
2
3
for(/* */){
//things that will be executed in every step
}
Last edited on
Thanks! The solution seems so obvious now. There is one more thing I can't figure out. The first line that prints,

if (i == 0)
cout << i << " " << "NULL" << " " << "||" << (i % lim == 0 ? '\n' : '\t');

is always on a line by itself while the rest prints out from 1 to 4 ASCII values as entered. Is this because of the way I designed the for loop?
1
2
3
4
if (i == 0)
cout << i << " " << "NULL" << " " << "||" << 
   (i % lim == 0 ? //i=0 so 0%lim==0 always (true)
      '\n' : '\t');
Topic archived. No new replies allowed.