• 0

[C++] Printing ASCII Tables


Question

Hello everyone, I currently have an assignment where I'm supposed to enter a character, and then a number, and in accordance it prints out the corresponding value.

 

For example:

 

Enter a character: 

input: a

Enter a number:

input: 5

 

output: abcdef

 

The above is the correct output. My program however, prints out what a5 would be on the table, so a little help! I'll include the sample program the professor provided as well. Thank you!

//Lab 19

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	int i1, answer;
	char c;

	cout << "Enter a character: ";
	cin >> c;

	for (int i = 1; i < 999; i++){
		cout << "\nEnter a number: ";
		cin >> i1;
		if (i1 <= 260){
			break;
		}
	}
	for (int i = 1; i <= i1; i++){
		c = i1;
		cout << c;
	}
	cout << endl;
	cout << "Run it back?";
	cin >> answer;
	if (answer == 'y' || answer == 'Y'){
		main();
	}
	else if (answer == 'n' || answer == 'N'){
		system("PAUSE");
		return 0;
	}
}

Example program: https://www.dropbox.com/s/o7qfirh0imou5ub/C%2B%2BLab19%20%28ASCII_Characters%29.exe

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Your question doesn't really sound specific to me, more a 'write-my-homework-for-me' question.

Can you give a bit more detail in what you have tried, where you think you're going wrong and what you actually want help with?

Link to comment
Share on other sites

  • 0

It's not clear what it is that you don't understand. I'll try to explain why your program doesn't work. 

for (int i = 1; i <= i1; i++) {
  c = i1;
  cout << c;
}
Here you are assigning to c only for the purpose of interpreting an integer as a character. The code is equivalent to: 

for (int i = 1; i <= i1; i++) {
  cout << (char)i1;
}
Therefore, this code prints the number [i1], interpreted as a character, [i1] times.

In your assignment, I see that the correct output is made of different characters. You are printing the same character several times. That's one mistake. It looks like you had the right idea with a for loop, but you're not using your iteration variable...

Secondly, this: 

c = i1;
cout << c;
simply reinterprets i1 as a character. If i1 is 1, then the character will be ASCII value 1, which is not a letter. You need to print the correct value, which should be based on the character that the user entered (hint: you're not even using the character value you got from the user).

****************************************

Let's start with an easier problem and work our way towards something as complex as that assignment.

"Let the user input a character. Then, print that character."

I'll write that one out for you:

char c;
cout << "Enter a character: ";
cin >> c;
cout << "Here is what you entered: ";
cout << c;
Sample run:

Enter a character: f

Here is what you entered: f

Ok now let's move on to a slightly more difficult problem.

"Let the user input a character. Then, print that character, and the next character in the ASCII table after it."

Same code, but now you have a blank to fill.

char c;
cout << "Enter a character: ";
cin >> c;
cout << "Here is what you entered and the next ASCII character: ";
cout << c;
cout << // fill in the blank
Sample run: 

Enter a character: f

Here is what you entered and the next ASCII character: fg

Ok then you can move on to something that looks more like your assignment:

"Let the user input a character. Then, print that character, and the next 10 characters in the ASCII table after it." 

char input;
cout << "Enter a character: ";
cin >> input;
cout << "Here is what you entered and the next 10 ASCII characters: ";
for (...) {
  cout << ...
}

Sample run:

Enter a character: a

Here is what you entered and the next 10 ASCII characters: abcdefghijk

If you can solve that, then it's just a matter of replacing 50 with another input from the user and you have pretty much done your assignment.

As a side note, do not call your main() function. Not only is this illegal C++, but you are creating a potentially infinite recursion (as many times as the user wants) which could terminate your program with a stack overflow. Just use a real loop.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.