ASCII character table. Help needed.
Jul 26, 2011 at 9:41pm UTC
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;
}
Jul 26, 2011 at 9:52pm UTC
if you wanted to print ten characters a line
set up a counter variable, not a loop.
increment the counter for each letter
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.
Jul 26, 2011 at 10:07pm UTC
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 Jul 26, 2011 at 10:07pm UTC
Jul 26, 2011 at 10:30pm UTC
@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
Jul 26, 2011 at 10:47pm UTC
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 Jul 26, 2011 at 10:47pm UTC
Jul 26, 2011 at 10:53pm UTC
Thank you, thats what i was looking for do you know how i could use setw to make it look more spacious.
Jul 28, 2011 at 3:36am UTC
1 2
#include<iomanip>
cout<<setw(4)<<//some var<<setw(4)<<//some other var<<//etc.
Topic archived. No new replies allowed.