I did this with strings, as I couldnt figure out how to do it with a char
array. I know strings can be beneficial, but my goal is to use strings, as
I've been unable to find how to do it with arrays with other exercises...
Please help!!
/*
This program asks the user for a student's name and grades, then prints the name along with the total
grade.
*/
#include <iostream>
#include <string>
usingnamespace std;
class Student
{
string name; //name of the student
string yourName;
int marks1 = 0, marks2 = 0; //Each of the student's marks(grades), zero is default
int total; //total of the student's marks
public:
string getName(string name); //receives the name from the user
int calc(int marks1, int marks2); //receives and adds together the student's marks (zero is default)
void disp(string yourName, int totalGrade); //displays the name and grade of the student
Student()
{
yourName = getName(name);
total = calc(marks1, marks2);
disp(yourName, total);
}
};
string Student::getName(string name)
{
cout << "Enter your name: ";
getline(cin, name);
return name;
}
int Student::calc(int grade1, int grade2)
{
cout << "Enter first grade: ";
cin >> grade1;
cout << "Enter second grade: ";
cin >> grade2;
int total = grade1 + grade2;
return total;
}
void Student::disp(string yourName, int totalGrade)
{
cout << endl;
cout << "Name: " << yourName << endl
<< "Total Grade: " << totalGrade << endl;
}
int main()
{
Student student1; //with default constructors, dont use ()
system("pause");
return 0;
}