I am trying to write a program with lists that stores student id's, names, resident status and credit hours. The program is supposed to accept input as long as the id# is not 0. The output is the list of students in the order they were enterd, the list of resident students, and the list of students with 12+ credit hours. I have the below that I have written. When I compile I get two errors. Also, what is the best way to make sure that the values are entered until id = 0? Right now, I have a break statement, but I think a while loop would be more efficient. However, I have never used a while loop and immediaitely after used a for loop. Is this the best way?
#include <iostream>
#include <string>
#include <iomanip>
#include <list>
usingnamespace std;
//==========================================================
// class to hold student information
class Student
{
private:
int id; // student ID
string name; // student name
char type; // 'R' represents resident and 'N' represents nonresident
int crds; // number of credits enrolled
public:
// default constructor
Student()
{
id = 0; name = ""; type = ' '; crds = 0;
}
// constructor
Student(int theId, string theName, char theType, int theCrds)
{
id = theId; name = theName; type = theType; crds = theCrds;
}
// get functions
int getID() {return id;}
string getName() {return name;}
char getType() {return type;}
int getCrds() {return crds;}
// prints out the student's record
void print()
{
cout << setw(10) << id << setw(15) << name
<< setw(15) << type << setw(10) << crds << endl;
}
};
//===================================================================
int main()
{
list<Student> stuList;
list<Student>::iterator iter;
int id;
string name;
char type;
int crds;
for(int i=0; i<3; i++)
{
cout << "Student ID: ";
cin >> id;
if(id=0)
{break;}
cin.ignore();
cout << "Student Name:";
getline(cin, name);
cout << "Student Type (R/N)";
cin >> type;
cout << "Number of Credits: ";
cin >> crds;
cin.ignore(80, '\n');
Student stu(id, name, type, crds);
stuList.push_back(stu);
}
cout << "Students in the order they were entered" << endl;
iter = stuList.begin();
while(iter != stuList.end())
{
(*iter).print();
cout << endl;
iter++;
}
cout << "Resident Students" << endl;
iter = stuList.begin();
while(iter != stuList.end())
{
if( (*iter).getType=='R')
{
(*iter).print();
cout << endl;
}
iter++;
}
cout << "Full Time Students (12+ Credit Hours)" << endl;
iter = stuList.begin();
while(iter != stuList.end())
{
if( (*iter).getCrds >= 12)
{
(*iter).print();
cout << endl;
}
iter++;
}
return 0;
}
Here are the errors:
1 2 3
stulist.cpp: In function 'int main()':
stulist.cpp:85: error: invalid use of member (did you forget the '&' ?)
stulist.cpp:96: error: invalid use of member (did you forget the '&' ?)
If you have the time, take a look at this article. I wrote this article a while ago as a way to show how some std library features can be easily used for these types of exercises. http://cplusplus.com/forum/articles/10879/