B1azingPh03nix
B1azingPh03nix

Reputation: 47

Uninitialized local variable error on a vector in C++

I'm trying to finish an assignment for my C++ class, and I've hit a problem that I haven't been able to find an answer to thus far.

What I'm trying to do is to create a vector that can grow indefinitely until the user breaks the loop. This vector must hold class objects to hold student names and grade information. I have the class working; the only part of the program that seems to be giving me trouble at this point is the vector.

I keep getting this error code:

error C4700: uninitialized local variable 'students' used

Here is my function:

void vectorfctn(){
   vector<student> *students; //I'm assuming this is what is causing the error
   string stud;
   double ex1;
   double ex2;
   double hw;
   double fex;
   char exit;

   do {
    cout << "Enter Student Name" << endl; cin >> stud;
    cout << endl << "Enter First Exam" << endl; cin >> ex1;
    cout << endl << "Enter Second Exam" << endl; cin >> ex2;
    cout << endl << "Enter Homework" << endl; cin >> hw;
    cout << endl << "Enter Final" << endl; cin >> fex;

    student* s1 = new student(stud, ex1, ex2, hw, fex);
    s1->calcFinalGrade();
    students->push_back(*s1); //This is the line referenced by visual studio in the error

    cout << "Would you like to continue? y or n" << endl;
    cin >> exit;
    delete s1;
   } while (exit != 'n');

for (size_t i = 0; i < students->size(); i++) {
    cout << students->at(i).calcFinalGrade() << endl;
}
};

How would I initialize my vector without limiting its size? I suck with vectors, and don't really understand them a whole lot, so any advice would be greatly appreciated.

Upvotes: 0

Views: 830

Answers (5)

Bill O&#39;Neill
Bill O&#39;Neill

Reputation: 41

You were creating a pointer to the vector (declaration) but not actually creating the vector (definition). As others have said, simply create the vector on the stack.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void vectorfctn() {
    vector<Student> students;

    string stud;
    double ex1;
    double ex2;
    double hw;
    double fex;
    char exit;

    do {  // todo: validate user input
        cout << "Enter Student Name" << endl;        getline(cin, stud);
        cout << endl << "Enter First Exam" << endl;  cin >> ex1;
        cout << endl << "Enter Second Exam" << endl; cin >> ex2;
        cout << endl << "Enter Homework" << endl;    cin >> hw;
        cout << endl << "Enter Final" << endl;       cin >> fex;

        //Student s1 = Student(stud, ex1, ex2, hw, fex);
        //s1.calcFinalGrade();  // does this really need to be here?  It's also used below in the for loop
        //students.push_back(s1);
        students.push_back(Student(stud, ex1, ex2, hw, fex));

        cout << "Would you like to continue? y or n" << endl; cin >> exit;

    } while (exit != 'n');

    for (auto student : students) { cout << student.calcFinalGrade() << endl; }
};

Upvotes: 0

lamandy
lamandy

Reputation: 982

The line vector *students; creates a pointer of vector and not the vector itself. When you are trying to call its member function via ->, there is no vector created yet, thus you are not able to push_back anything inside and thus the error.

To fix this, just remove * at the vector *students; and replace all students->push_back with students.push_back

Upvotes: 1

user2486888
user2486888

Reputation:

First, declare the vector object, instead of pointer.

vector<student> students;

Second, declare the student locally, instead of pointer and new.

student s1(stud, ex1, ex2, hw, fex);

Third, push_back.

students.push_back(s1);

Upvotes: 1

r-sniper
r-sniper

Reputation: 1483

students vector doesn't need to be pointer.
Replace vector<student> *students; with vector<student> students;

and

students->pushback() to students.pushback()

Upvotes: 3

NL628
NL628

Reputation: 438

Use push_back to append whatever you want to the vector.

vector<int> myVec;
myVec.push_back(2);
myVec.push_back(3);

I think your syntax for push_back is incorrect. Did this help?

Upvotes: -1

Related Questions