Grade Increase Question. Enum?

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.

My attempt at a solution is as follows:

#include <iostream>
#include <cstring>
#include <string>

int main()
{
using namespace std;
string name;
string lastname;
char gradearray[] = {'A', 'B', 'C', 'D'};
enum grade {A, B, C, D};
grade request;
int age;

cout << "What is your first name?\n";
getline(cin, name);
cout << "What is your last name?\n";
getline(cin, lastname);
cout << "What letter grade do you deserve?\n";
cin >> request;
cout << "What is your age?\n";
cin >> age;
cout << "Name: " << lastname << ", " << name << endl;
cout << "Grade: " << gradearray[request + 1] << endl;
cout << "Age: " << age << endl;
}

I'm having trouble with increasing the grade. Tried using enum. but my enum variable "request" doesn't seem to be able to use the cin function.

Any help would be appreciated!

Cheers!
Unicyclist
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.

Thank you sammy and maikel! The solution works perfectly...

Its a book with a blue cover.. but i'm using the latest edition. I think the 4th edition had a paint brush on it =)

Thanks again!


Cheers!
Unicyclist
Topic archived. No new replies allowed.