help for a question

Hi, I am new c++ programmer. I just learned about pointers, strings, arrays, getline() etc. I was solving this problem:
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
.
Here's the code I came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  // info.cpp -- information about people
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
  char first[20], last[20], grade[2];
  int age;
  cout << "What is your first name? ";
  cin.getline(first, 20);
  cout << "what is your last name? ";
  cin.getline(last, 20);
  cout << "What letter grade do you deserve? "; 
  cin >> grade;
  cout << "What is your age? ";
  cin >> age;
  cout << "Name: " << last << ", " << first << endl;
  cout << "Grade: " << grade << endl;
  cout << "Age: " << age << endl;
  return 0;
}

I realise that the grade needs to be adjusted downward, I don't understand how to do it. Any help would be appreciated.
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
#include <iostream>
#include <cstring>

int main()
{
    // avoid magic numbers in code
    // see: http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants
    const int NAMESIZE = 20 ;

    char first[ NAMESIZE ], last[ NAMESIZE ] ; // , grade[ 2 ];
    char grade ; // grade is a single character
    int age;

    std::cout << "What is your first name? ";
    std::cin.getline( first, NAMESIZE );

    std::cout << "what is your last name? ";
    std::cin.getline( last, NAMESIZE );

    std::cout << "What letter grade do you deserve? ";
    std::cin >> grade;

    std::cout << "What is your age? ";
    std::cin >> age;

    std::cout << "Name: " << last << ", " << first << '\n' ; // endl;

    // adjust the grade downward
    char adjusted_grade = 'F' ;
    if( grade == 'A' || grade == 'a' ) adjusted_grade = 'B' ;
    else if( grade == 'B' || grade == 'b' ) adjusted_grade = 'C' ;
    else if( grade == 'C' || grade == 'c' ) adjusted_grade = 'D' ;
    std::cout << "Grade: " << adjusted_grade << '\n' ; // endl;
    
    std::cout << "Age: " << age << '\n' ; // endl;

    // return 0; // return 0 here is implicit
}
I'm sorry, but is there any other way to adjust the grade. I haven't learned about if, else and or yet.
Not without going into the ASCII values of the characters themselves. For purposes here, the letter 'A' has no logical connection with 'B', so how would a program know to "downgrade" a certain letter to another certain letter. Also, you learned about pointers before if statements?

Here's how you would increment a char
1
2
3
    char c = 'A';
    c++;
    std::cout << c << std::endl; //prints B 
A's ASCII value is 65.
B's ASCII value is (conveniently) 66.
You could do that instead of the if statements. In a program more complicated though, incrementing a char as a value would probably just be confusing..
Last edited on
Yeah, I learned about pointers before if statements. Thanks for the ASCII value idea, i'll use it.
Topic archived. No new replies allowed.