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
//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;
}
// 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;
}