The problem states to use a loop display character from the ASCII codes 0 through 127 and to display 16 character per line.

I am using Borland C++ 4.5 so I need the older programming way of doing it.

This is what i have so far.

#include <iostream.h>

int main()

{




	for (char letter = 0; letter < 127; letter++)
	{
		for (int num = 0; num <= 16; num++)
		{
		cout << letter;

		if (num == 16)
		break;
		}
		cout << endl;
	}


return 0;

}

What this does is repeat the same ASCII character over and over on a line and goes to the next line and repeats. What I need is to have the ASCII character not repeat and show all character 0-127 which should only take 8 lines.

Eeek Borland... I used it in a class over 12+ years ago...

The structure of your first for-loop is correct, but I am not sure that you can use 'char' as the type to do that. You need to use 'int' and start from 1, not 0. I believe that you cannot display the first 16 chars as normal display. You will hear a beep when it hits #7.

Now, you just need another variable declared outside the loop and may name it chrCount. Initiate it to 0 as well. Then inside the loop, increment it by 1 at the beginning of the loop. After that, check if the value of chrCount is equal to 16. If it is, print out a new line and reset the value to 0. There is no else statement for this if. The rest of the content in the loop is to display the number as ASCII. I cannot remember what function you need to display that. Maybe it is call chr(#)???

Well I need to use 'char' to display the ASCII characters or else it will just display numbers.

This is what I got now

#include <iostream.h>

int main()

{


	int chrCount = 0;

	for (char letter = 1; letter < 127; letter++, chrCount++)
	{

		cout << letter;

		if (chrCount == 16)
		break;
		}
		cout << endl;



return 0;

}

Now it just shows the the first characters for some lines and stops, doesn't show any letters.

That's because you're breaking out of the loop after 16 characters. Instead, when you hit 16
1) reset the character counter
2) output the ENDL

Oh I see that makes sense, I was using the wrong example in the book.

#include <iostream.h>

int main()

{


	int chrCount = 0;

	for (char letter = 1; letter < 127; letter++, chrCount++)
	{

		cout << letter;

		if (chrCount == 16)
		cout << endl;
		}

		cout << endl;


return 0;

}

That is what I have and now the outcome shows the all the characters but for only four lines, it doesn't skip a line after 16 characters.

You still need to reset the chrCount to 0 inside your if-statement. Oh and display the letter after the if-statement, not before.

Oh ok, forgot that part XP

Ok now this is what I got

#include <iostream.h>

int main()

{


	int chrCount = 0;

	for (char letter = 1; letter < 127; letter++, chrCount++)
	{



		if (chrCount == 16)
		chrCount = 0;
		cout << endl;

		cout << letter;
		}

		cout << endl;


return 0;

}

The output comes out with all the ASCII characters it ends the line after each so I'm think I have to make two different loops?

{ }?

As WaltP said... You need {} to create the scope of if-statement. If you use if-statement without that, only the immediate code line right after 'if' will be executed under the 'if' condition.

omg lol

I saw the solution but idk why I didn't think it was it.

#include <iostream.h>

int main()

{


	int chrCount = 0;

	for (char letter = 1; letter < 127; letter++, chrCount++)
	{



		if (chrCount == 16)
		{
		chrCount = 0;
		cout << endl;
		}

		cout << letter;
		}

		cout << endl;


return 0;

}

Thank You guys so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.