Jump to content



Photo

[C++] Printing ASCII Tables


  • Please log in to reply
2 replies to this topic

#1 coolbunny1234

coolbunny1234

    Neowinian

  • Joined: 18-March 14
  • Location: Boston
  • OS: Windows 8
  • Phone: Galaxy S3

Posted 24 April 2014 - 18:59

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....Characters).exe




#2 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 24 April 2014 - 19:22

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?



#3 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 25 April 2014 - 01:54

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.



Click here to login or here to register to remove this ad, it's free!