Hi there!
I'm trying to learn C++ on my own and am using the book C++ Primer Plus. So far I've learnt about data types, compound types, pointers (intro) and the iostream. So I'm trying to do this without using "if" statements yet.
Kinda stuck on one question:
Write a C++ program that requests and displays information as shown in the following
example of output:
What is your first name? Betty Sue
What is your last name? Yew
What letter grade do you deserve? B
What is your age? 22
Name: Yew, Betty Sue
Grade: C
Age: 22
Note that the program should be able to accept first names that comprise more than one
word. Also note that the program adjusts the grade downward—that is, up one letter.
Assume that the user requests an A, a B, or a C so that you don’t have to worry about the
gap between a D and an F.
I think I read that book once too! Does it have a white cover with a paint brush on it?
Anyway...you can handle the grade using a char. Simply read from cin and store the value into request, which is a char. You then use the fact that chars can be interpreted as integers; you can add one to it, and set it back as a char :).
1 2 3 4 5 6 7 8
...
char request;
...
cout << "What letter grade do you deserve?\n";
cin >> request;
...
cout << "Grade: " << (char)(((int)request) + 1) << endl;
...
A question for you: what would happen if someone entered "twenty" for the age? You'd probably have an issue in this case. Keep these things in mind as you're reading the book...you can't trust users to always do the right thing and you hence have to provide a means to validate their input :).
You already learned in Chapter 3 about ASCII code and you should already have done the excercises about ASCII. That is why sammys solution is simply the correct one.
Maikel
PS: if you need help in solving more of the excercises, feel free to ask, because i did already all of them and have solutions on my computer.