We have talked a bit about how chars are really just ASCII integers. Let's see this in action. We will show the characters starting at ASCII 33 and end with a user-supplied number. Use a for loop to output the characters with two spaces between each.
Prompt:
This program will output the ASCII characters starting at 33
Enter the integer for the last ASCII character you wish to see: [user types: 43]
Output:
! " # $ % & ' ( ) * +
Notes and Hints:
1) You MUST use a FOR LOOP
2) Hint: Which operator from Chp 3 allows us to convert from one data type to another?
3) FYI only: http://www.asciitable.com/
//Solution-1
using namespace std;
main(void)
{
int StartChar = 33,EndChar,n;
cout<<"\nEnter the last character ASCII (decimal): "; cin>>EndChar;
cout<<"\n\nCharacters from ASCII = "<<StartChar<<" to ASCII = "<<EndChar<<endl;
for(n=int(StartChar);n<=int(EndChar);n++)
{
cout<<char(n)<<" ";
}
return(0);
}
Solution-2
cout<<char(n)<<" ";
The above statement converts ASCII code to character and displays the equivalent character.
Answer to this question
Send
Comments
Leave a comment