Hey, im new to the forum and im trying to learn c++, so i bought a book.

On a programming exercise i was asked to create a program that would take a persons full name, letter grade, then subtract the letter grade (ie: A to B), and age and display it on the screen.

Everything was working fine until i tried to work out the part to subtract the letter grade.

Here is the link to the programming exercise page (no.1): http://books.google.com/books?id=zuyAIZ9ZIskC&pg=PA153&lpg=PA153&dq=c%2B%2B+primer+plus+betty+sue&source=bl&ots=mbx4P-t2HX&sig=4nn3Mqs9YUMY25BcvYC9EN_fQTs&hl=en&ei=HPRFTI3dI4vSsAOgsoCyAg&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBYQ6AEwAA#v=onepage&q&f=false

My code:

#include <iostream>
#include <string>
int main()

using namespace std;
string name;
string lastname;
char grade[6] = {"ABCDF"};
char gradez [1];
cout << "What is your first name?";
getline(cin, name);
cout << "What is your last name?";
getline(cin, lastname);
cout << "What letter grade do you deserve?";
cin >> gradez;
gradez = grade[] + 1;
cout << "Name:" << lastname << name<<endl;
cout << "Grade: " << gradez;
return 0;

Everything works fine untill i try to match what letter grade the user typed in to the different grades on the array (fourth or fifth line from the bottom).

Please help me figure this out.


I'm not exactly sure what the problem is. Does it crash? Or what is the input, current output, and expected output?


I don't understand how "gradez = grade[] + 1;" is supposed to "try to match what letter grade the user typed". This makes absolutely no sense to me!

well, doing "gradez++;" will effectively turn an 'A' into a 'B' and so on. I guess that line was mistyped on this forum post.

With that I don't see anything wrong really. If gradez does not come out well maybe you can try the cin.getc() function instead. And possibly the "cout << gradez" outputs in decimal instead of as a single character string.

The variable gradez shouldn't be an array. All you have to do is declare it and initialize it to NULL.

char aChar = '\0';

Then, after you get it from the user, verify that it's either 'A' 'B' or 'C' and simply increment it:

cin >> aChar;
aChar += 1;

The array "grade" is completely irrelevant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.