1.11M Members

I need help in correcting my codes

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

It's customary that one provides some accompanying information.

 
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.

 
0
 

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

 
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.

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

Thanks much, I have some basics now

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