Homework 6: ASCII Exploration
The goal of this assignment is to educate you about how computers store character data, how char can be treated like int and how to teach the computer to make simple decisions based on the facts you provide it.
Concepts
If you stay up all night knitting, you will be sleepy in class. If you are sleepy in class, your instructor will call on you. If you get called on and you did your reading, you will proudly state the answer; otherwise, you will be embarassed and go back to sleep.
If your program has a character called Susan who stayed up all night knitting after she read Etter section 3.3, what will happen to her? How can we teach the computer how to model this scenario?
Or consider this: "Game over. Play again?" How can we teach the computer to accept what you tell it and to take particular action?
Now that you've learned about how the machine thinks about truths and falsehoods, it's time to teach it how to make decisions based on these ideas.
Teaching the Computer How to Make Decisions Based on the Facts You Provide It
In software engineering, we call this ability "flow control" and specifically "selection statements." But you really should consider it to be your ability to program the machine such that it is able to make decisions. Have you ever had a friend who was terribly indecisive? It's annoying, isn't it? Machines aren't like that; in fact, they're decisive to a fault, so to speak.
You should have learned from your reading and your instructor's lecture about how to use if constructs to model decision making. "So what," you think, "all the machine can do is make binary decisions, how boring. The choices I have to make every day are far more complex," and you would be partially right.
Individual binary decisions on their own are indeed boring, and yes, you do make more complex decisions every day ("Should I take the double-black or the groomer?"). However, what you need to recognize is this: every so-called complex decision you make can be reduced to a set of binary decisions that, when evaluated, yield the answer that appears in your mind. As such, in theory we can teach the machine to make any decision a human would make -- we just need to model it as a set of boolean expressions.
The next time someone asks you on a date, well, answer them first, but afterwards go back and think about how you made the decision. "Is (s)he cute? Does he seem nice? Is he funny? Is he rich? Does he smell bad? Does he like to program? How desparate am I?" Trust us, a gazillion boolean expressions are processed by your mind every day. We can definitely teach a computer whether or not it should say yes or no when asked out. Of course, a computer being asked out on a date would be another problem. Besides, computers don't date seriously -- they're jerks.
How Computers Represent Character Values (or, the Secret Love Affair Between char and int)
You should have learned from your reading and your instructor's lecture about how characters are actually stored as integers. Rather than bore you with some details, let's take a look at some code examples.
char letter;
cin >> letter; // pretend the user types a
number = letter;
cout << "letter: " << letter << endl; // prints "letter: a"
cout << "number: " << number << endl; // prints "number: 97"
Holy poop! Did you see that? We captured a character value 'a' from input and also assigned that value to an int. Then we asked cout to print both variables and for the char variable it printed a letter and for the int variable it printed a number. Now take a look at this:
cout << "letter: " << letter << endl; // prints "letter: c"
Whoah! We assigned an integer value to a char and when we asked cout to print that char it printed the character value whose numeric code is 99.
Now take a look at this:
cin >> letter; // pretend the user types c
letter = letter - 32;
cout << "downcased: " << letter << endl; // prints "downcased: c"
OMG! How can we subtract (or add) to character values? One more example:
cout << "Game over. Play again? (Y or N)" << endl;
cin >> playerChoice;
if (playerChoice == 'N') {
cout << "See ya!";
} else if (playerChoice == 'Y') {
replay();
} else {
cout << "I asked you a Yes or No question buddy!";
}
Whoah! I can use relational operators like == and < or > with characters!
If you want to capture the user's input from the keyboard and do something with it (start a game or move Mario on the screen) you need to understand the ASCII table. Don't memorize it, just note it's structure:

We'll leave you with one more example as a hint for your homework.
cout << "Enter a letter" << endl;
cin >> playerInput;
if ( (playerInput >= 'a' && playerInput <= 'z') ||
(playerInput >= 'A' && playerInput <= 'Z') ) {
cout << "Thanks!";
} else {
cout << "Well tanks for nutchin!";
}
That code above checks to see if the user entered an alpha character (a - z or A - Z) and acts accordingly.
Instructions
To exercise your understanding of character values and simple selection statements, write a program that exhibits the following behavior:
Enter any character on the keyboard: q
The ASCII code for q is 113.
It is lower case.
Do not #include <Windows.h> or color your output. Colors above are merely to draw your attention.
Your assignment should ask the user for one character as input (shown in orange above) and must print its numeric value as well as whether or not it is upper case, lower case, numeric, or non-alphanumeric.
Requirements and Rubric
Your program must only prompt for input once.
Your program must output two specific lines. One of those lines must take the form:
Where X is the character typed by the user and Y is an integer corresponding to the character's ascii numeric value.
The second required line of output must take the form:
Where Z is any one of "lower case", "upper case", "numeric", or "non-alphanumeric". Spelling is important, so be accurate and check your work.
Anything else your program "says" is up to you.
This work is worth 26 points.
| Requirement | Points | Notes |
|---|---|---|
| Correct submission of src directory as a .zip file. | 1 | |
| Place your name in the comment header in main.cpp | 1 | |
| Correct datatypes used | 2 | char |
| Correctly prompts for only one input | 2 | |
| Correctly prints the numeric value of the character | 4 | |
| Correctly prints the type of character | 8 | lower case, upper case, numeric, non-alphanumeric |
| Proper stylistic formatting of selection statement(s) | 8 | legible, uniform indentation and spacing |
Concepts Exercised: compilation, I/O, making decisions, declaring facts
© 2011 Yong Joseph Bakos, Keith Hellman