You want to get the input and set the member variables:
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
void input();
float GPA() const;
void Display() const;
private:
string name;
int id;
int grade1;
int grade2;
int grade3;
};
int main()
{
student kyle(name, id, grade1,grade2,grade3);
kyle.Display();
return 0;
}
void student::input()
{
cout<<"enter student id number"<<endl;
cin>>id;
cout<<"enter student name"<<endl;
cin>>name;
cout<<"enter three grades"<<endl;
cin>>grade1;
cin>>grade2;
cin>>grade3;
}
float student:: GPA() const
{
return (grade1 + grade2 + grade3) / 3;
}
void student:: Display() const
{
cout<<" Student Gradepoint average is: "<< GPA() <<endl;
}
Note the syntax changes and the function prototype change.
firstPerson
Industrious Poster
4,046 posts since Dec 2008
Reputation Points: 851
Solved Threads: 626
Skill Endorsements: 15
I think firstPerson meant to do this with his line 22.
student kyle;
kyle.input();
Create an object with the default constructor. Then fill it in with the input function, which should take NO parameters.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
There's no need to have a function input() like this:
void student::input(string name, int id, int grade1, int grade2, int grade3)
{
cout<<"enter student id number"<<endl;
cin>>id;
cout<<"enter student name"<<endl;
cin>>name;
cout<<"enter three grades"<<endl;
cin>>grade1;
cin>>grade2;
cin>>grade3;
}
when you have your private members like the function's arguments:
string name;
int id;
int grade1;
int grade2;
int grade3;
The arguments you provide to your input() function will become the primary variables of your function (enteres the scope of the function), thus calling cin>>grade3; will actually insert a value into the argument grade3, which will be lost when the function will exit, and also, not writing anyting concrete to your class members... furthermore, calling your Display() function will print only the text, with no result whatsoever.
I need help in correcting my codes
Well, you actually do. But first, you'll have to learn some basic things about classes, before you actually start using them. Here are some links:
Click Here
Click Here
As for your code, you'll probably understand your errors after reading those links, but here it goes anyway:
#include<iostream>
#include<string>
using namespace std;
class student
{
string name;
int id;
int grade1;
int grade2;
int grade3;
public:
void input();
const float GPA() const;
void Display();
};
int main()
{
student object; //creating an object of type student
object.input(); //calling methods on the newly created object
object.GPA();
object.Display();
return 0;
}
void student::input()
{
cout<<"enter student id number"<<endl;
cin>>id;
cin.ignore();
cout<<"enter student name"<<endl;
getline(cin, name);
cout<<"enter three grades"<<endl;
int ar[3];
for (int i=0;i<3;i++){
cout<<"Grade["<<i+1<<"]: ";
cin>>ar[i];
}
grade1=ar[0];grade2=ar[1];grade3=ar[2];
}
const float student:: GPA () const
{
return (float)(grade1 + grade2 + grade3)/3;
}
void student:: Display()
{
cout<<"Student:\nId: "<<id<<"\nName: "<<name<<"\nGradepoint average: "<< GPA()<<endl;
}
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12