1,105,633 Community Members

I need help in correcting my codes

Member Avatar
dachy12
Newbie Poster
3 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#include<iostream>
#include<string>
using namespace std;

class student
{
public:
       void input(string name, long id, int grade1, int grade2, int grade3);
       void GPA(float) const;
       void Display() const;

private:
       string name;
       int id;
       int grade1;
       int grade2;
       int grade3;
};

int main()
{
       string name;
       int id;
       int grade1,grade2,grade3; 
       float gradepoint;
       student input(name, id, grade1,grade2,grade3);
       student GPA();
       student Display();

       return 0;
}

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

void student:: GPA(float) const
{
       float gradepoint;
       int grade1, grade2, grade3;

       gradepoint = (grade1 + grade2 + grade3) / 3;

}

void student:: Display() const
{
       float gradepoint;
       cout<<" Student Gradepoint average is: "<< gradepoint<<endl;
}
Member Avatar
Suzie999
Posting Pro in Training
461 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

It's customary that one provides some accompanying information.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

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.

Member Avatar
dachy12
Newbie Poster
3 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

M getting errors here when i ran the codes saying undeclared indentifiers
student kyle(name, id, grade1,grade2,grade3);

Member Avatar
VernonDozier
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]
Featured
 
0
 

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.

Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
1
 

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;
}
Member Avatar
dachy12
Newbie Poster
3 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks much, I have some basics now

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article