Hi
Please help me with the code given below. I'm getting the errors alsp given at the bottom. The code also have two embedded questions. Please help me with them too. Thank you for your help.
Best wishes
Jackson
Code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
/////////////////////////////////////////////////////
struct Date {int d; string m; int y;};
////////////////////////////////////////////////////
/////////////////////////////////////////////////////
class Student
{
private:
int rollno;
string sex;
string name;
float gpa; //you cannot do, say, "cout << stud[1].gpa;"
Date DoB;
public:
void prnt();
void read();
char grade(float dummyGpa) const; /* what does "const" do here? */
public:
Student(){int rollno; string& sex, string& name; float gpa; Date& DoB};
};
/////////////////////////////////////////////////////
const int L = 2;
int main()
{
Student stud[L]; int i;
for(i=0; i<L; i++)
{
cout << "enter student #" << (i+1) << "'s details below:-" << endl;
stud[i].read();
stud[i].prnt();
cout << endl << endl;
}
/* this cannot be executed because "gpa" is
private, "cout << stud[1].gpa << endl;" */
//cout << stud[1].gpa.read() << endl;// how do I access the GPA of stud[1]?
system("pause");
return 0;
}
//-----------------------------------------------
// Student read() definition
void Student::read()
{
Student stud;
cout << "enter name: "; getline(cin, name);
cout << "enter roll number: "; cin >> rollno;
cout << "enter sex: "; cin >> sex;
cout << "enter date of birth (e.g. 01 Jan 2000) below:-" << endl;
cout << "enter day: "; cin >> DoB.d;
cout << "enter month: "; cin >> DoB.m;
cout << "enter year: "; cin >> DoB.y;
cout << "enter GPA: "; cin >> gpa;
cin.ignore();
}
//-------------------------------------------------
// void prnt() definition
void Student::prnt()
{
cout << "\n\n\n************************************\n\n\n";
cout << "roll no.: " << rollno << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "date of birth: " << DoB.d << "-" << DoB.m << "-" << DoB.y << endl;
cout << "grade: " << grade(gpa) << endl;
cout << "\n\n\n************************************";
}
//-------------------------------------------------------------
// grade() definition
char Student::grade(float dummyGpa) const
{
if (dummyGpa >= 3.5)
return 'A';
else if (dummyGpa >= 3.0)
return 'B';
else if (dummyGpa >= 2.5)
return 'C';
else if (dummyGpa >= 2.0)
return 'D';
else
return 'E';
}
//----------------------------------------------------
Errors:
Code:
|In constructor 'Student::Student()':|
32|error: 'sex' declared as reference but not initialized|
32|error: expected initializer before '&' token|
32|error: expected initializer before '}' token|
||=== Build finished: 3 errors, 0 warnings ===|