#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include "Course.h"
#include <list>
using std::list;
#include <cstdlib>
#include <iomanip>
using std::setw;
int main()
{
// declare the list
list<Course> myCourses;
// variables
char choice;
char buf[100];
// start a loop
while(true)
{
// prompt the user to ADD a node or QUIT
cout << "Enter a node? (Y/N):";
cin >> choice;
cin.ignore(1000, 10);
if(choice == 'N' || choice == 'n') break; // if QUIT, break out of the loop
// ADD a node (queue method -- add to end) ----------------------------------
Course c;
// FILL node with data from user
cout << "Enter course name (up to 10 chars): ";
char name[11];
cin.getline(name, 11);
c.setName(name);
cout << "Enter term (i.e. SP2013): ";
char term[7];
cin.getline(term, 7);
c.setTerm(term);
cout << "Enter units: ";
char unit;
cin >> buf; unit = atoi(buf);
c.setUnit(unit);
cout << "Enter grade (X if class is not completed): ";
char grade;
cin >> grade;
c.setGrade(grade);
// INSERT at end of list (queue method) ------------------------------
myCourses.push_back(c);
// PRINT list ------------------------------------
list<Course>::iterator p;
// print out heading
cout.setf(ios::left, ios::adjustfield);
cout << endl;
cout << setw(12) << "COURSE" << setw(8) << "TERM"
<< setw(7) << "UNITS" << setw(5) << "GRADE" << endl;
cout << setw(12) << "----------" << setw(8) << "------"
<< setw(7) << "-----" << setw(5) << "-----" << endl;
for(p = myCourses.begin(); p != myCourses.end(); p++)
c.printMe();
cout << endl;
}
}
The program works well, except for the fact when it prints out the list (starting at the for-loop on line 65) each line is the same as the last, like it overwrites itself every time it traverses the while(true) loop.
I had a feeling it might be something like that. Now the question is, how do I print the whole list? What I'm printing are private members so I need to use the printMe function to access the data members.
I'm storing the "name" and "term" as char arrays, unit as int, and grade as char. I would show you the header file, but I'm not at my computer. Ill post it tonight at around 11PST if it is still necessary.
Ill try and recall from memory:
class Course
{
char name[11];
char term[7];
int unit;
char grade;
public:
printMe();
// functions for getting private data members listed above