Having trouble printing out list using OOP

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
59
60
61
62
63
64
65
66
67
68
69
#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.

Any help would be greatly appreciated!
Last edited on
Your problem is line 66. You're printing c each time through the loop.
You should be printing what the interator points to.

 
  p->printMe();

Last edited on
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.
Edit: pointless suggestion...

You code for printing out the list looks ok, but it should probably be moved outside of the while loop.

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
// snip

int main()
{
  // declare the list
  list<Course> myCourses;

  // variables
  char choice;
  char buf[100];

  // start a loop
  while(true)
  {
    // snip

    // 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;

  return 0;
}

Last edited on
While that wouldn't be a bad idea, the requirements for this assignment state that the list must be printed after the addition of every new node.
Ah, ok -- sorry!

Out of interest, how are you storing the values in the class?

Actually, we could probably do with seeing the declaration of your class, Course.

Andy
Last edited on
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

}
Last edited on
Sorry, looked right through it before...

It's just that you should be using p-> rather than c. to call printMe()

1
2
  for(p = myCourses.begin(); p != myCourses.end(); p++)
    p->printMe(); // not c.printMe(); 


You were iterating correctly, but rather than calling printMe() on the iterated object, you were calling it repeatedly on the last object created.

Andy
Last edited on
Thanks! That did it! I tried p.printMe(); but I forgot that p is a pointer and needs an arrow.
Note that p is not a pointer, it's an iterator.

How are iterators and pointers related?
http://www.cplusplus.com/forum/general/102393/

Iterator definitions
http://www.cplusplus.com/reference/iterator/

Andy
Topic archived. No new replies allowed.