Removing elements from vector

Hi I have some homework and I am unsure of how to finish this part of the code. I need to remove an element from my vector, but I am unsure of how to do it.
On input #3 is were I am currently having difficulties.
I need help on how to do remove a course from the vector.
also when i try to change the course when the student has 2 courses it always changes the first course even if the user inputted the name of the second course.
My Entire code
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  #include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct course //course structure
{
    string coursename; //coursename
    int markachieved; //markachieved
};

struct Student //student structure
{
    string name; //student name
    int ID; //student id
    int numberCourses; //number of courses student is taking
    vector <course> coursetrack; //course vector for student

};

vector <Student> studentstructures; //Student vector

int main()
{
    cout<<"Welcome to the class tracker!"<<endl; //Display welcome message
    while (true) //while input is true
    {
        int input,numberCourses=0,markachieved=0,ID=0,n=0,inputtwo; //integer variables
        double average=0,newmark; //average variable
        string name,coursename,coursenametwo; //string variables
        cout<<"Choose an option."<<endl; //display option menu
        cout<<"1) Open a class from a file"<<endl;
        cout<<"2) Add a new student"<<endl;
        cout<<"3) Change existing student"<<endl;
        cout<<"4) Delete a student"<<endl;
        cout<<"5) Print a class list (includes the class average)"<<endl;
        cout<<"6) Save the class to a file"<<endl;
        cout<<"7) Quit"<<endl;
        cin>>input; //recieve user input
        cin.ignore(); //ignore white spaces
        if(input==1) //if user input is 1
        {
            ifstream InFile("Class.txt"); // locate text file
            if (InFile.fail()) //if text file is not there
            {
                cout << "File: "<< "Class.txt" << " not found"<<endl; //display error message
            }
            else //if found
            {
                string line; //create a string variable
                while(getline(InFile,line)) // getline returns false at EOF
                {
                    cout << ">" <<line << endl; //display each line in text file
                }
                cout << "ALL DONE"  << endl; // output completion message
            }
        }
        if (input==2) //if user input is 2
        {
            Student a; //create a student
            cout<<"Input the name of the student."<<endl; //ask for name of student
            getline(cin, a.name); //receive name
            cout<<"Input the ID of the student."<<endl; //asl for ID of Student
            cin>>a.ID; // recieve ID
            cout<<"Input the number of courses the student is taking."<<endl;//ask user for number of courses student is taking
            cin>>a.numberCourses; //recieve number of courses
            a.coursetrack.resize(a.numberCourses); //resize the vector
            // cout<<"Size: "<<a.coursetrack.size()<<endl; //check
            for(int i=0; i<a.numberCourses; i++) //for all the number of courses the student is taking
            {
                cout<<"Course name?"<<endl; //ask course name
                cin>>a.coursetrack[i].coursename; //recieve course name
                cout<<"Mark Achieved?"<<endl; //ask mark achieved
                cin>>a.coursetrack[i].markachieved; // recieve mark achieved
            }
            cout<<a.name<<","<<a.ID<<","<<a.numberCourses; //display the studen name,ID,number of courses
            for(int i=0; i<a.numberCourses; i++) //for all the students courses
            {
                cout<<","<<a.coursetrack[i].coursename<<","<<a.coursetrack[i].markachieved; //display coursename,mark achieved
            }
            studentstructures.push_back(a); //Add Student a, to end of student structure vector
            cout<<""<<endl;
        }
        if(input==3)
        {
            string cname;
            cout<<"Enter the name of the student you want to change."<<endl; //ask for name to change information
            getline(cin, cname); //receive name
            for (int i=0; i<studentstructures.size(); i++) //for the size of the student structure vector
            {
                if(cname==studentstructures.at(i).name)
                {
                    cout<<"1) Change a course"<<endl;
                    cout<<"2) Remove a course"<<endl;
                    cout<<"3) Add a course"<<endl;
                    cin>>inputtwo;
                    if(inputtwo==1)
                    {
                        cout<<"Which course do you want to change?"<<endl;
                        cin>>coursenametwo;
                        for(int q=0; q<studentstructures.at(i).coursetrack.size(); q++) //for the size of the coursetrack vector
                        {
                            if(coursenametwo==studentstructures.at(i).coursetrack[q].coursename)
                            {
                                cout<<"Enter the new mark."<<endl;
                                cin>>studentstructures.at(i).coursetrack[i].markachieved;
                            }
                        }
                    }
                    if(inputtwo==2)
                    {
                        
                    }
                }

            }
        }

        if (input==5) //if user inputs option 5
        {
            cout<<"Name      |  ID  |         Course and Mark "<<endl; //display guidline
            cout<<"_______________________________________________"<<endl; //border
            for (int i=0; i<studentstructures.size(); i++) //for the size of the student structure vector
            {
                cout<<studentstructures.at(i).name<<"         "<<studentstructures.at(i).ID<<"        "; //display user name and id
                for(int q=0; q<studentstructures.at(i).coursetrack.size(); q++) //for the size of the coursetrack vector
                {
                    cout<<studentstructures.at(i).coursetrack[q].coursename<<","; //display coursename
                    cout<<studentstructures.at(i).coursetrack[q].markachieved<<","; //display markachieved
                }
                for(int a=0; a<studentstructures.at(i).coursetrack.size(); a++) //for all the marks
                {
                    average +=(studentstructures.at(i).coursetrack[a].markachieved); //add all marks together
                    n++; //count number of marks
                }

                cout<<""<<endl;
            }
            average=average/n; //calculate average by sum of all marks divided by number of marks
            //    cout<<"n is:" <<n<<endl;//check
            cout<<"The average of the class is: "<<average<<endl; //display average of class
        }
        if(input==6) //if user inputs 6
        {
            ofstream OutFile ("Class.txt"); // locate class.txt
            if(OutFile.fail()) //if not found
            {
                cout << "File: "<< "Class.txt" << " not saved"<<endl; //display error message
            }
            else//if found
            {
                for (int i=0; i<studentstructures.size(); i++) //for the size of the student structure vector
                {
                    OutFile<<studentstructures.at(i).name<<"         "<<studentstructures.at(i).ID<<"        "; //display user name and id
                    for(int q=0; q<studentstructures.at(i).coursetrack.size(); q++) //for the size of the coursetrack vector
                    {
                        //cout<<"Num: "<<studentstructures.at(i).coursetrack.size()<<endl;
                        OutFile<<studentstructures.at(i).coursetrack[q].coursename<<",";//display coursename
                        OutFile<<studentstructures.at(i).coursetrack[q].markachieved<<","; //display markachieved
                    }
                    OutFile<<""<<endl;
                }

                OutFile.close(); // close the file when done
            }
            cout<<"Class Saved!"<<endl; //display completion message
        }
        if(input==7) //if user inputs 7
        {
            cout<<"Thank you for using the class tracker"<<endl; //display goodbye message
            cout<<"Have a good day"<<endl; //display goodbye message
            break; //break loop
        }
    }
    return 0;
}
//end of program 
Last edited on
for removing items from a vector review this -> http://www.cplusplus.com/reference/vector/vector/erase/
Topic archived. No new replies allowed.