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');
[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
}