I am trying to display the sNames vector. I am not sure if I am storing the information correctly. Any help would be greatly appreciated. Also I am trying to get an average for each students exam grades and a class average, and I am not sure how to go about this.

#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>


using namespace std;

struct Student{
      
       string fName;
       string lName;
       vector<double>exams;
};

           
int main(){
    int count;
    const int NUM_EXAMS = 5;
    Student tempFName, tempLName;
    double tempExam;
    int numStudents = 0;
    vector<Student> sNames;
    vector<Student>::iterator iter;
    
       
    cout << "Number of students: " << endl;
    cin >> numStudents;
     
    for (int i = 0; i < numStudents; i++){
         Student student;
         cout << "Enter the first name for student #" << i+1
             << ": " << endl;
         cin >> student.fName;
          sNames.push_back(student); 
          cout << "the first name of the student is: " << student.fName << endl;
          
         cout << "Enter the last name for student #" << i+1
             << ": " << endl;
         cin >> student.lName;
         sNames.push_back(student); 
          cout << "the first name of the student is: " << student.fName << endl; 
             
             for (int j = 0; j < NUM_EXAMS ; j++ ){
                 cout << "Enter the exam scores student #" << i+1 << " exam #" << j + 1
                 << ": " << endl;;
                 cin >> tempExam;
                 student.exams.push_back(tempExam); 
                 sNames.push_back(student);
             }// end of nested for loop
       //cout << sNames.student.fName << student.lName <<  endl;   
    }//end of for loop
      
    //display the vector
    
     for (count = 0; count < sNames.size(); count++){
       cout << sNames[count] << " ";
         cout << endl;
    }// end of for loop
          
     for (int i = 0; i < sNames.size(); i++){
         cout << sNames.size() << " " ;
         cout << endl;
     }// end of the for loop


    _getch();
    return 0;
    
}//end of main

Recommended Answers

All 3 Replies

Your problem is (simplified) this:

for (int i = 0; i < numStudents; i++)
{
  Student student;
  student.fName="fred";
  sNames.push_back(student);    // Copy the current version to sNames
 
  // Do stuff with student ... doesn't matter what.   
   
  // student goes out of scope
}

The problem is the push_back uses a copy constructor to copy the state of the object to the vector. It does not mean that it is tied to the object. If you did this

Student A;
A.fName="fred";
sNames.push_back(A);
A.fName="john";
sNames.push_back(A);

// sNames would be of size 2, and have two Student objects, one called fred and the other called john.

In direct answer to the question:

There are many ways to write out a vector. Here are two examples which do not use a functional method. [You have used both loops and iterators in you code, hence my choice]:

for(size_t i=0;i<sNames.size();i++)
  {
    std::cout<<"Item "<<i<<"=="<<sNames[i].fName<<endl;
  }
// or
std::vector<Student>::const_iterator iter;
for(iter=sNames.begin();iter!=sNames.end();iter++)
  {
    std::cout<<"Item =="<<iter->fName<<endl;
  }

Hope this helps.

It should also be noted, in addition to StuXYZ's excellent comments, that you should get an entire Student before adding a copy of it to the vector.

Your choice of variable names is pretty good, actually, but I will nit-pick about the vector name: "sNames" -- first, it isn't a vector of student names, it is a vector of student data, which includes the name. A good name for a collection of Student is "students".

So, remember, the order to do things is:

  1. Get all the information about a student into a single Student object.
  2. Use push_back() to append a copy of that Student object to the vector of Students.
  3. Repeat.

Also, I notice you have variables starting with the prefix "temp". You never use them, so get rid of them. (Delete lines 21 and 22.)

Remember, a Student is not just a name. It is a collection of information about one particular student: his first name, last name, and all his grades.

// Here is the list of students we wish to populate.
vector<Student> students;

...

// This loop gets 'numStudents' students from the user.
for (int i = 0; i < numStudents; i++) {

    // This is the temporary variable we'll use to hold information
    // about a single student as we get it from the user.
    Student student;

    // Now, gather the individual pieces until we have them all.

    cout << "Enter the first name for student #" << (i + 1) << ": ";
    cin >> student.fName;

    cout << "Enter the last name for student #" << (i + 1) << ": ";
    cin >> student.lName;

    cout << "Enter the exam scores for student #" << (i + 1) << endl;
    for (int j = 0; j < NUM_EXAMS; j++) {

        // Just like we do for the students, this
        // object represents a single exam grade.
        double examScore;

        // Get it from the user
        cout << "  Exam #" << (j + 1) << ": ";
        cin >> examScore;

        // We have a complete exam grade, so we append a copy
        // of it to the list of exams for the current student.
        student.exams.push_back( examScore );
    }

    // At this point, we have gathered all the information about a single student.
    // Append a copy of the student's information to our list of students.
    students.push_back( student );
}

Now, to display the student's information, you need a function that knows how to write a Student structure to the screen using cout:

void cout_Student( const Student& student ) {
    // Displays the student as:
    //
    //   Jenny Jones 98 100 89.3 95 100
    //
    cout << student.fName << " " << student.lName;
    for (int n = 0; n < student.exams.size(); n++)
      cout << " " << student.exams[ n ];
    cout << endl;

    // This, of course, is just an example. You can organize your
    // function to display the student information however you like.
}

In your code, you can now display all the students with a simple loop:

for (int n = 0; n < students.size(); n++) {
    cout_Student( students[ n ] );
}

If you are ready, you can turn that function into an overloaded insertion operator. If you don't know what that means, then worry about that later. Your professor will get there in due time.

Hope this helps.

It works great, Thanks so much for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.