By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,124 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,124 IT Pros & Developers. It's quick & easy.

C++ Vector problem

P: 8
This may be a fairly simple fix but I'm new to C++ and hoping you guys can help, I'm creating a Student database in C++ and use a Vector called students which is of type Student, this is made up of Name and Student Id so a string and an int.

I have already created the system too add them, I have an enrolement.cpp where all the main code goes then a studentlist.cpp where the add search remove etc is done.

Now I'm trying to list all students, of course if it was a single type of vector like a string vector I could use a for loop like
Expand|Select|Wrap|Line Numbers
  1. for(int i; i<students.size;i++){
  2. cout << students.at(i) << endl
  3. }
  4.  
But it hates this because it cannot convert from type student to that.

It's wracked my brains for a few days can anyone give me a hand?
Dec 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: 30
try
for(int i; i<students.size;i++)
{
cout << students[i].Name <<" "<< students[i].Student_Id<<endl
}


i had a similar projec a few weeks ago and if you need extra help just ask :)
Dec 25 '06 #2

P: 8
Thanks for the help, the thing is almost complete now although an addition problem came from courses, each student has a list of courses they can be on and these courses are stored in a Vector with course name and course code. I figured how to add a course to a specific student through


Expand|Select|Wrap|Line Numbers
  1.             int idCheck;
  2.             Student *student;
  3.             cout << "Enter Student Id" << endl;
  4.             cin >> idCheck;
  5.             student = StudentList.search(idCheck);
  6.  
  7.             //If statement here for if found
  8.  
  9.             cout << "Enter Course Name" << endl;
  10.             getline(cin,courseName); 
  11.             cout << "Enter Course Code" << endl;
  12.             cin >> courseCode;
  13.  
  14.             Course newCourse(courseName,courseCode);
  15.             CourseList.add(newCourse);
  16.             student->Enrol(CourseList);    
  17.  

Expand|Select|Wrap|Line Numbers
  1. void Student::Enrol(CourseList courseListIn)
  2. {
  3.     this->courses=courseListIn;
  4.  
But now I need to accomplish 2 things:
1: Print the Student's details along with a list of courses they are on, trying to call the vector within the vector confuses me greatly and so has stumped me
2: Save the information on the students, I had this working perfectly but now that it needs to be saved with the courses also I have a feeling it's going to be hard. All I can think of is setting the program to print for example "%%%" to seperate the students from their courses and then perhaps "$$$$" to seperate for a new student when the file is saved so when loaded it can be searched for these values to seperate what is read and can be placed in each corret variable (So a course name doesn't become a student name)


What I have so far for 1
Any help would be GREATLY appreciated
Expand|Select|Wrap|Line Numbers
  1.             int idCheck;
  2.             Student *student;
  3.             cout << "Enter ID" << endl;
  4.             cin >> idCheck;
  5.             student = studentList.search(idCheck);
  6.  
  7.  
  8.             if (student != NULL) 
  9.             {
  10.               // code here for printing
  11.             } 
  12.             else 
  13.             {
  14.  
  15.             }
  16.  
  17. }
  18.  
  19.  
Dec 28 '06 #3

P: 30
Thanks for the help, the thing is almost complete now although an addition problem came from courses, each student has a list of courses they can be on and these courses are stored in a Vector with course name and course code. I figured how to add a course to a specific student through


Expand|Select|Wrap|Line Numbers
  1.             int idCheck;
  2.             Student *student;
  3.             cout << "Enter Student Id" << endl;
  4.             cin >> idCheck;
  5.             student = StudentList.search(idCheck);
  6.  
  7.             //If statement here for if found
  8.  
  9.             cout << "Enter Course Name" << endl;
  10.             getline(cin,courseName); 
  11.             cout << "Enter Course Code" << endl;
  12.             cin >> courseCode;
  13.  
  14.             Course newCourse(courseName,courseCode);
  15.             CourseList.add(newCourse);
  16.             student->Enrol(CourseList);    
  17.  

Expand|Select|Wrap|Line Numbers
  1. void Student::Enrol(CourseList courseListIn)
  2. {
  3.     this->courses=courseListIn;
  4.  
But now I need to accomplish 2 things:
1: Print the Student's details along with a list of courses they are on, trying to call the vector within the vector confuses me greatly and so has stumped me
2: Save the information on the students, I had this working perfectly but now that it needs to be saved with the courses also I have a feeling it's going to be hard. All I can think of is setting the program to print for example "%%%" to seperate the students from their courses and then perhaps "$$$$" to seperate for a new student when the file is saved so when loaded it can be searched for these values to seperate what is read and can be placed in each corret variable (So a course name doesn't become a student name)


What I have so far for 1
Any help would be GREATLY appreciated
Expand|Select|Wrap|Line Numbers
  1.             int idCheck;
  2.             Student *student;
  3.             cout << "Enter ID" << endl;
  4.             cin >> idCheck;
  5.             student = studentList.search(idCheck);
  6.  
  7.  
  8.             if (student != NULL) 
  9.             {
  10.               // code here for printing
  11.             } 
  12.             else 
  13.             {
  14.  
  15.             }
  16.  
  17. }
  18.  
  19.  

" the vector inside a vector "
for ex:
you have the class:

class student
{ public :
string name;
string id;
vector<course> courses;
}

class cours
{
public:
string name;
string id;
}

and
vector<student> students;

to write and read students name and id is simple just use students[i].name;
or students[i].id
and reading && writing to a vector in a vector (course id , name ) just use
students[i].courses[j].name; and students[i].courses[j].id; simple
i counts the students and j counts the courses

an for saving try to write every piece o information on separate line:

student 1 name
student 1 id
student 1 course 1 id
student 1 course 1 name
student 2 name
.......................
...........
and now locate the students bether by countig the lines
Dec 28 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.