A simple solutiom that evades my grasp...

I am working on the first exercise problem at the end of ch4 in C++ Primer Plus:
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.


The problem I am having is the adjustment of the grade. The book has not touched control or logic structures so I know it can be done without a loop or if statement. I'm assuming I need to make an array for it but I am unsure as to how. Perhaps an enumeration might work as well. Any thoughts or fingers pointing me towards that plateau of understanding would be helpful, lol

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
39
40
41
42
43
44
45
46
//Exercise 1

#include <iostream>
#include <cstring>

struct school
{
       char firstName[20];
       char lastName[20];
       char usrGrade;
       int userAge;     
};

int main()
{   
    using std::cin;
    using std::cout;
    using std::endl;
    
    //create a struct variable to access members in struct
    school persInfo; 
    
    //prompt for personal info and the send to struct
    cout << "What is your first name? ";
    cin.getline(persInfo.firstName, 20);
    cout << endl;
   
    cout << "What is your last name? ";
    cin.getline(persInfo.lastName, 20);
    cout << endl;
    
    cout << "What kind of grade should you get? ";
    cin.get(persInfo.usrGrade);
    cout << endl;
    
    cout << "How old are you? ";
    cin >> persInfo.userAge;
    cout << endl;
    
    //Send info back to the screen
    cout << "Name: " << persInfo.lastName << ", "
         << persInfo.firstName << endl;
    cout << "Grade: " << persInfo.usrGrade + 1 << endl;
    cout << "Age: " << persInfo.userAge << endl;
       
}


Last edited on
Maybe this pointer will point you in the right direction.
http://www.danshort.com/ASCIImap/

-Albatross
Hint: 'A'-1=='B'.
EDIT: forgot to quote helios.
Hint: 'A'-1=='B'.


Uh... that evaluates to false.

Did you mean: 'A'+1 == 'B', which evaluates to true?

EDIT2: You're going to have a problem when you get to D...

-Albatross
Last edited on
Uh... Yeah.
Thanks for the input. I told you it was simple. Simple enough that I should have been able to figure that out alone, lol.
Char problem

I agree with you :-

Thanks for the input. I told you it was simple. Simple enough that I should have been able to figure that out alone, lol.



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// grades.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//Exercise 1

#include <iostream>
#include <cstring>

struct school
{
       char firstName[20];
       char lastName[20];
       char usrGrade;
       int userAge;     
};

int _tmain(int argc, _TCHAR* argv[])
{   
    char prsEnter;
    using std::cin;
    using std::cout;
    using std::endl;
    
    //create a struct variable to access members in struct
    school persInfo; 
    
    //prompt for personal info and the send to struct
    cout << "What is your first name? ";
    cin.getline(persInfo.firstName, 20);
    cout << endl;
   
    cout << "What is your last name? ";
    cin.getline(persInfo.lastName, 20);
    cout << endl;
    
    cout << "What kind of grade should you get? ";
    cin.get(persInfo.usrGrade);
    cout << endl;
    
    cout << "How old are you? ";
    cin >> persInfo.userAge;
    cout << endl;
    
    //Send info back to the screen
    cout << "Name: " << persInfo.lastName << ", "
         << persInfo.firstName << endl;
    cout << "Grade: " << (char)(persInfo.usrGrade + 1) << endl;
    cout << "Age: " << persInfo.userAge << endl;
 
	cin>>prsEnter;
}
Last edited on
Topic archived. No new replies allowed.