In: Computer Science
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/
// CONSTANTS AND VARIABLES
Starter Code:
// INPUT
std::cout << "This program will output the ASCII characters
starting at " << YOUR_CODE << std::endl;
std::cout << "Enter the integer for the last ASCII character
you wish to see: ";
std::cout << std::endl; // For Mimir
// LOOP and OUTPUT
std::cout << YOUR_CODE << " ";
Hello!
Here for this problem we have to use type conversion or type casting.
Type casting: Conversion of data type to another data type.
There are 2 types of type casting.or type conversion
1.Implicit type casting.
2. Explicit type casting.
Implicit conversion:
In this conersion the complier automatically converts the data type to another type
Example:
int i = 33;
char c = i;
cout << c; // so it prints the equivalent ascii for 33 which is !
Another example when we want to add double and int the int data is converted to double automatically, so that there will no loss of data. Complier converts smaller data type to larger data type.
Explicit conversion:
Here we force the complier to convert one data type to another data type.
This we can achive in 2 types:
1.
int i = 66
cout << (char) << endl; // so it prints the equivalent ascii for 66 which is B
2.
int i = 69
cout << (char) << endl; // so it prints the equivalent ascii for 69 which is E
SO based on the above concept we can achive the output you have given
Please try to understand the code and it will be very easy after reading the concept. if you have any doubts please feel free to comment , I will help you in 15 min.
METHOD 1:
CODE:
#include <iostream>
int main() {
int initial,final;
initial = 33;
int YOUR_CODE = initial;
// INPUT
std::cout << "This program will output the ASCII characters starting at " << YOUR_CODE << std::endl;
std::cout << "Enter the integer for the last ASCII character you wish to see: ";
std::cin >> final;
std::cout << std::endl; // For Mimir
// LOOP and OUTPUT
for(YOUR_CODE=initial;YOUR_CODE<=final;YOUR_CODE++)
std::cout << (char)YOUR_CODE << " ";
return 0;
}
OUTPUT:
Method 2:
CODE:
#include <iostream>
int main() {
int initial,final;
initial = 33;
int YOUR_CODE = initial;
// INPUT
std::cout << "This program will output the ASCII characters starting at " << YOUR_CODE << std::endl;
std::cout << "Enter the integer for the last ASCII character you wish to see: ";
std::cin >> final;
std::cout << std::endl; // For Mimir
// LOOP and OUTPUT
for(YOUR_CODE=initial;YOUR_CODE<=final;YOUR_CODE++)
std::cout << static_cast<char>(YOUR_CODE) << " ";
return 0;
}
OUTPUT:
THE SAME OUTPUT AS IN METHOD 1
Hope this helps and clear.
I strive to provide the best of my knowledge so pleamse upvote if you like the content.
Thank you!