Linked Lists Question

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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;
  }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  do{
    course *p=new course;
    cout << "Enter a course code: (e.g. COMSC-101)" << endl;
    cin >> p->courseName;
    cout << "Enter a term: (e.g. FA2015)" << endl;
    cin >> p->term;
    cout << "Enter the amount of units: (e.g. 4)" << endl;
    cin >> buf; p->units=atoi(buf.c_str());
    cout << "Enter the grade you received: ('X' if the course is in progress)" << endl;
    cin >> p->grade;

    p->next = start;
    start = p;
    printList(start);

    cout << "\nWould you like to add another course? (Y/N)" << endl;
    cin >> addNewCourse;

  }while(toupper(addNewCourse)=='Y');
Topic archived. No new replies allowed.