954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Homework Help with ASCII Characters

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.

Lando_
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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(#)???

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

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.

Lando_
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

Lando_
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

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?

Lando_
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

{ }?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

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!

Lando_
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: