Hey I am learning C++ from the book "C++ Primer Plus" and I am on an exercise that has me write a program to write a program to ask from a persons full name(has to be able to handle names with 2 words), age, and what grade they think they earned. The program then will print out the name, age, and the grade that is one letter lower. My entire code is this.
Code:
#include <iostream>
#include <string>
struct info
{
std::string first;
std::string last;
int age;
char grade;
};
int main()
{
using namespace std;
info student;
cout<< "What is your first name? ";
getline(cin, student.first);
cout<< "What is your last name? ";
getline(cin, student.last);
cout<< "What letter grade do you deserve(A-C only)? ";
cin >> student.grade;
cout<< "What is your age? ";
cin >> student.age;
student.grade = char(int(student.grade) + 1);
cout<< "Name: " << student.last << ", " << student.first << endl;
cout<< "Age: " << student.age << endl;
cout<< "Grade: " << student.grade << endl;
return 0;
}
What I was wondering is there a "more correct" way to get a multi word string as input besides this?
Code:
getline(cin, student.first);
It is working for what I need to but I just do not want to develop any habits that later I will have to change.
Also another thing that I was wondering is there an better way to firgure out the final grade is besides this?
Code:
student.grade = char(int(student.grade) + 1);
If there is anything else you notice that is worth mentioning, I would be extremly thankful for anyone who would point it out.