ASCII character table. Help needed.

How would i get this program to print only ten characters per line?

#include <iostream>
using namespace std;

int main()
{
for (int i = 33; i < 127; i++)
{
std::cout << i << ": " << (char)i << std::endl;
}
return 0;
}
if you wanted to print ten characters a line

set up a counter variable, not a loop.
 
int count = 0;


increment the counter for each letter
 
count++;


remove the endl from your print line and put it in an if like
1
2
3
4
5
6
7
      if(count < 10)
      {
               // reset the count
               count = 0;
               // dump the endl;
               std::cout << std::endl;
      }


I will let you figure out the rest, but that is the basic logic.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
   for (int i = 33; i < 127; i++)
   {
      std::cout << i << ": " << (char)i << std::endl;
      if ((i - 32) % 10 == 0)
         cout << endl;
   }
   return 0;
}


In my condition I'm saying, make the value start at 1, after each 10 characters add an new line.
Last edited on
@Turbine

I ran your code and it put them in groups of ten but they still were all on the same line. I need to have 10 per line kinda like rows in a table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// the correction to his code is

#include <iostream>
using namespace std;

int main()
{
   for (int i = 33; i < 127; i++)
   {
      std::cout << i << ": " << (char)i;
      if ((i - 32) % 10 == 0)
         cout << endl;
   }
   return 0;
}


This might be what you are looking for.
Last edited on
Thank you, thats what i was looking for do you know how i could use setw to make it look more spacious.
1
2
#include<iomanip>
cout<<setw(4)<<//some var<<setw(4)<<//some other var<<//etc. 
Topic archived. No new replies allowed.