Sol Sol - 16 days ago 4
C++ Question

How can you output an OOP Array in C++?

I use the online c++ editor https://www.tutorialspoint.com/compile_cpp_online.php and wasn't sure why It gives me this error:

main.cpp: In function 'int main(int, char**)':
main.cpp:87:127: error: invalid use of non-static member function
cout<<setw(20)<<student[i].getLastName()<<setw(20)<<student[i].getFirstName()<<setw(20)<<fixed<<setprecision(2)<<student[i].getGPA<<endl;

main.cpp:90:16: error: 'system' was not declared in this scope
system("PAUSE");


It points at the student[i].getFirstName() and I am not sure why. To me the syntax looks ok, but I am not sure if I am missing something?

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student {
private:
string sFirstName;
string sLastName;
float sGPA;

public:
Student();
void setFirstName(string name);
void setLastName(string address);
void setGPA(float gpa);
string getFirstName();
string getLastName();
float getGPA();
};

Student::Student()
{
sFirstName="";
sLastName="";
sGPA=0.00;
}

void Student::setFirstName(string fName)
{
sFirstName = fName;
}

void Student::setLastName(string lName)
{
sLastName = lName;
}

void Student::setGPA(float gpa)
{
sGPA=gpa;
}

string Student::getFirstName()
{
return sFirstName;
}

string Student::getLastName()
{
return sLastName;
}

float Student::getGPA()
{
return sGPA;
}


//int main(int argc, char** argv) {
int main(int argc, char** argv) {
//int size =10;
Student student[3];
string tempString;
string tempString2;
float tempFloat;
int tempInt;
for(int i = 0; i <3; i++)
{
cout<<"Please enter student's first name: "<<endl;
cin>>tempString;
cout<< "Please enter student's last name: "<<endl;
cin>>tempString2;
cout<< "Please enter student's first name:" <<endl;
cin>>tempFloat;

student[i].setFirstName(tempString);
student[i].setLastName(tempString2);
student[i].setGPA(tempFloat);
}

//20
cout<<setw(20)<<"Last Name"<<setw(20)<<"First Name"<<setw(20)<<"GPA"<<endl;
for(int i = 0; i<3; i++)
{
cout<<setw(20)<<student[i].getLastName()<<setw(20)<<student[i].getFirstName()<<setw(20)<<fixed<<setprecision(2)<<student[i].getGPA<<endl;

}
system("PAUSE");
return 0;
}

Answer

In this statement

   cout<<setw(20)<<student[i].getLastName()<<setw(20)<<student[i].getFirstName()<<setw(20)<<fixed<<setprecision(2)<<student[i].getGPA<<endl

use

student[i].getGPA()

instead of

student[i].getGPA

and include header <cstdlib>

#include <cstdlib>