a little help please

hello, I must be having Alzheimer's for the moment because I just cant seem to remember how to put something in a row. Here's the problem that I am doing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*Write a program that uses a loop to display the characters for ASCII codes 0
  through 127, Display 16 characters on each line.*/
#include <iostream>
using namespace std;

int main()
{
	char letter;

	cout << " I will display the characters for ASCII codes 0 through 127.\n";

	letter = 0;

	for(int count = 0; count <= 127; count++)
	{
		cout << letter << "  " << endl;
		letter++;
	} 
	
	return 0;
}


The only problem is putting 16 characters on a single line, please help
Hi

You could use the modulo operator (%) to check if count is at a multiple of 16 and if so output a newline e.g.

1
2
3
4
5
6
7
8
9
	for(int count = 0; count <= 127; count++)
	{
		if (count % 16 ==0)
           		cout << endl; 
		   
		        cout << letter << "  ";
			letter++;
		
	} 


By the way, are you definitely supposed to be printing the characters 0 to 127? The first 32 characters are control characters.


Last edited on
Thanks for your help. And yes, the problem is for me to go from 0 to 127.
Topic archived. No new replies allowed.