The purpose of this program is to allow a user to input data about classes they've taken and the program will display a comprehensive list of all the classes.
My issue is that after a new class is added, the print function should print out all previously added classes but it isn't. Is there something wrong with the for loop or am I missing some linked list idea?
Thanks for the help!
Edit: I realized my comments didn't make it over. Added them back for context.
#include<iostream>
using std::cout;
using std::endl;
using std::string;
using std::cin;
using std::ios;
#include<iomanip>
using std::setw;
#include<cstdlib>
using std::atoi;
struct course{
string courseName;//e.g. COMSC-165
string term;//FA2016
int units;
char grade;
course *next;//tail
};
void printList(course*);
int main()
{
course *start=NULL;//initialize head of node
char addNewCourse;//for adding new courses
string buf;//for user input
do{
course *p=new course;//make pointer to a new object
cout << "Enter a course code: (e.g. COMSC-101)" << endl;
cin >> p->courseName;//get first data value
cout << "Enter a term: (e.g. FA2015)" << endl;
cin >> p->term;//get second data value
cout << "Enter the amount of units: (e.g. 4)" << endl;
cin >> buf; p->units=atoi(buf.c_str());//et third data value
cout << "Enter the grade you received: ('X' if the course is in progress)" << endl;
cin >> p->grade;//get fourth data value
printList(p);//print user input
cout << "\nWould you like to add another course? (Y/N)" << endl;
cin >> addNewCourse;//check if user wants to add another course
if (toupper(addNewCourse)=='Y')
p->next=start;//if adding a new course, initializes next value.
else
p->next=0;//if all done adding courses
}while(toupper(addNewCourse)=='Y');
}
void printList(course *p)
{
cout.setf(ios::left, ios::adjustfield);
cout << setw(11) << "COURSE" << setw(8) << "TERM" << setw(8) << "UNITS" << setw(8) << "GRADE" << endl;
cout << setw(11) << "______" << setw(8) << "____" << setw(8) << "_____" << setw(8) << "_____" << endl;//table header
for (course *temp=p; temp; temp=temp->next){//output course data
cout << setw(11) << temp->courseName << setw(8) << temp->term << setw(8) << temp->units << setw(8) << temp->grade << endl;
}
}