Trying to print toString from object held in vector

This is a discussion on Trying to print toString from object held in vector within the C++ Programming forums, part of the General Programming Boards category; Apologies in advance if this is a really dumb question, but I'm pretty new at C++. Anyway, I have a ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    Trying to print toString from object held in vector

    Apologies in advance if this is a really dumb question, but I'm pretty new at C++. Anyway, I have a Student object that has the following toString:

    Code:
    string Student::toString() const {
        return studentName + "\t" + studentProgramCode;
    }
    Various Student objects are stored in a vector (within StudentRecord.cpp) defined as this:

    Code:
    vector<Student> students;
    I am currently trying to print out all of the Student objects in the vector, using their toString. However, I'm rather stuck. I so far have this:

    Code:
    void StudentRecord::print() const {
         int i;
         for(i=0; i < students.size(); i++) {
             cout << students[i] << endl;
         }
    }
    I know the students[i] part is the bit that's wrong, but I can't work out what to put there. Thanks in advance for your help.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    students[i].toString()
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Thank you! I knew it was a silly question.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by whiskedaway View Post
    Thank you! I knew it was a silly question.
    You may think about how to modify your Student class in a way that the original StudentRecord::print() function will work...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. Reverse Print Vector Array help
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21