-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii.cpp
More file actions
43 lines (37 loc) · 1.32 KB
/
Copy pathascii.cpp
File metadata and controls
43 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* TODO: write a small program that prints an *ASCII table*. That is,
* it should print each character along side its numeric representation
* in the ASCII system: * https://en.wikipedia.org/wiki/Ascii
* A snippet of sample output might look like this:
* ...
* 65 :: A
* 66 :: B
* 67 :: C
* ...
* NOTE: you can trick cout into printing the ascii value of a character
* simply by type-casting to integer (or by storing the character in an
* integer variable and printing that instead). Also note that the printable
* range of ASCII values starts at 32 (space) and goes to 126.
* */
#include <iostream>
using std::cin;
using std::cout;
int main()
{
/* your answer goes here... */
char x; // Declare a character variable to hold user input int num;
cin >> x; // cin >> num
int y = static_cast<int>(x); // Convert the ASCII value to its corresponding integer value
// char ch = static_cast<char>(num); other way around
cout << x << " :: " << y << std::endl;
int n;
cin >> n;
char m= static_cast<char>(n);
cout << n << " :: " << m << std::endl;
// Print the ASCII table for printable characters (from 32 to 126)
for (int i = 32; i <= 126; ++i) {
// Cast the integer to a char and print both the integer and the character
cout << i << " :: " << static_cast<char>(i) << std::endl;
}
return 0;
}
// vim:foldlevel=2