Weird bug in this database program

Hello! I recently registered this account but I've been a long time lurker of these forums. I'd just like to start off with a big thanks because you guys definitely helped me out in the past from the past forum threads (that I leeched information off of =P).

I'd like to start off with I'm not a new to coding. But my main area of interest is in server side scripting for websites. I am, however, new to C++ and although I've never run into a roadblock before (learning C++, not other languages), this bug just doesn't make any sense to me. I hope you guys can help me out. I'll post the code below, then I'll go ahead and explain when and how this bug occurs.

One last thing before I start to explain the problem, the base code for this I actually found in the forums (not sure where exactly, it's been a couple days since I found it). Another user asked for help regarding his database and I used a lot of the information, including the style in which the database is constructed, and incorporated it into my own. Hope that isn't an issue.
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
179
180
181
182
183
184
185
186
187
188
189
190
//main().cpp
#include<iostream>
#include<fstream>
#include<limits>
#include<vector>
#include<string>
#include"students.h"
using namespace std;
int main() // Where Program Begins
{
    vector<Students*> database; //Vector for database
    bool quit=false;
    ofstream outFile("out.txt"); //saving database in out.txt
    int input,select1,j=0,age,exp,grade,grade2,quiz1,quiz2,hw1,hw2,as1,as2,as3,as4,as5,project; //defining all int variables
    string firstName;string lastName;string major; string search; string id; //defining all string variables
    while(!quit) //Prints the database before ever single iteration of the program
    {
        cout<<"Database size:"<<database.size()<<endl;
        cout<<"Database contains:\n"<<endl;
        for(int k=0;k<database.size();k++)
        {
            database[k]->printStats();
            cout<<endl<<endl;
        }

        cout<<"\n1. Add a Student\n2. Delete a Student\n3. Save The Database\n4. Display Specific Data \n5. Exit The Database\n";
        cin>>select1;
        cout<<endl;
        switch(select1)
        {
        case 1: // I broke this case into two cases for unknown grade and overall grade
            cout<<"\n1. Do you know his or her overall grade? \n2. Do you not know his or her overall grade?" << endl;
            cin>>input;
            cin.ignore();
            switch(input)
            {
            case 1: // Overall grade
                cout<<"\nEnter first name:";
                getline(cin,firstName);
                cout<<"\nEnter last name:";
                getline(cin,lastName);
                cout<<"\nEnter Student ID:";
                cin>>id;
                cout<<"\nEnter Age:";
                cin>>age;
                cout<<"\nEnter Major:";
                cin>>major;
                cout<<"\nEnter Grade:";
                cin>>grade;


                // Inserts the database entry generated directly into the program via ID location.
                if(database.size() < 1)
                {
                    database.push_back(new PM1(firstName,lastName,id,age,major,grade));
                    cout << "\nif runs";
                }
                else if (database.size() == 1)
                {
                    int i = 0;
                    if(database[i] -> getId() < id)
                        {
                            database.insert(database.begin() + 1, new PM1(firstName,lastName,id,age,major,grade));
                            cout << "\nelse if if == runs";
                        }
                    else
                        {
                            database.insert(database.begin(), new PM1(firstName,lastName,id,age,major,grade));
                            cout << "\nelse if else == runs";
                        }
                }
                else if (database.size() > 1)
                {
                    for(int i = 0; i < database.size(); i++)
                    {
                        if(database[i] -> getId() < id && database[i + 1] -> getId() >= id)
                        {
                            database.insert(database.begin() + i + 1, new PM1(firstName, lastName, id, age, major, grade));
                            cout << "\nelse if if runs";
                        }
                        else
                            {
                                database.push_back(new PM1(firstName,lastName,id,age,major,grade));
                                cout << "\nelse if else runs";
                            }
                    break;
                    }
                }
                else
                {

                cout << "\nDidn't work!";


                }

                break;
            case 2: // Case 2 is not completely completed yet, it works but I have not yet included the same insertion method as Case 1
                cout<<"\nEnter first name:";
                getline(cin,firstName);
                cout<<"\nEnter last name:";
                getline(cin,lastName);
                cout<<"\nEnter Student ID:";
                cin>>id;
                cout<<"\nEnter Age:";
                cin>>age;
                cout<<"\nEnter Major:";
                cin>>major;
                cout<<"Enter Quiz One value:";
                cin>>quiz1;
                cout<<"Enter Quiz Two value:";
                cin>>quiz2;
                cout<<"Enter Homework One Value:";
                cin>>hw1;
                cout<<"Enter Homework Two Value:";
                cin>>hw2;
                cout<<"Enter Project Grade Value:";
                cin>>project;
                cout<<"Enter Assignment One Value:";
                cin>>as1;
                cout<<"Enter Assignment Two Value:";
                cin>>as2;
                cout<<"Enter Assignment Three Value:";
                cin>>as3;
                cout<<"Enter Assignment Four Value:";
                cin>>as4;
                cout<<"Enter Assignment Five Value:";
                cin>>as5;

                database.push_back(new Grade(firstName,lastName,id,age,major,quiz1,quiz2,hw1,hw2,project,as1,as2,as3,as4,as5));
                break;
            }
            break;
        case 2: // Deleting entry rule
            cout<<"Enter Student ID to be deleted:";
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            getline(cin,search);
            for(int l=0;l<database.size();l++)
            {
                if(database[l]->getId()==search)
                {
                    database.erase(database.begin()+l);
                    break;
                }
            }
            break;
        case 3: // Saving to out.txt

            for(int i=0;i<database.size();i++)

            {

                database[i]->save(outFile);

                cout<<endl;

            }

            break;
        case 4: // Locates database entry via searching ID

            cout<<"Enter Student ID To Display Data:";
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            getline(cin,search);
            for(int l = 0; l < database.size(); l++)
            {
                if(database[l] -> getId() == search)
                {
                    database[l] -> printStats();
                    cout << endl;
                    break;
                }
            }
            break;

        case 5: //Exits program

            cout<<"\nExiting............";

            quit=true;

            break;

        }

    }

}



Continued in next post...(message too long =()
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
#ifndef STDENTS_H
#define STUDENTS_H
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Students //Overall Class
{
public:
    Students(string firstName,string lastName,string id)
    {mFirstName=firstName;mLastName=lastName;mId=id;}

    virtual void printStats()

    {

        cout<<"First name:"<<mFirstName<<endl;

        cout<<"Last name:"<<mLastName<<endl;
        cout<<"ID:"<<mId<<endl;
    }
    virtual void save(ofstream& outFile)

    {

        outFile<<"First name:"<<mFirstName<<endl;

        outFile<<"Last name:"<<mLastName<<endl;

        outFile<<"ID:"<<mId<<endl;

    }

    string getId()

    { return mId;}

protected:

    string mFirstName;

    string mLastName;

    string mId;

};
class PM1: public Students // This class is used in Case 1 of the very first option

{

public:

    PM1(string firstName,string lastName,string id,int age,string major, int grade):Students(firstName,lastName,id)

    {mAge=age;mMajor=major;mGrade=grade;}

    void Save(ofstream& outFile)

    {

        Students::save(outFile);

        outFile<<"Age:"<<mAge<<endl;

        outFile<<"Major:"<<mMajor<<endl;

        outFile<<"Grade:"<<mGrade<<endl;

    }

    void printStats()

    {

        Students::printStats();

        cout<<"Age:"<<mAge<<endl;

        cout<<"Major:"<<mMajor<<endl;

        cout<<"Grade:"<<mGrade<<endl;

    }

protected:

    int mAge;

    string mMajor;

    int mGrade;

};
class Grade: public Students //This class is used in clas 2 of the very first option

{

public:

    Grade(string firstName,string lastName,string id,int age,string major,int quiz1, int quiz2, int hw1, int hw2, int project, int as1, int as2, int as3, int as4, int as5):Students(firstName,lastName,id)

    {mAge=age;mMajor=major;mGrade2=(.1*(quiz1+quiz2)+.25*(project)+.4*(as1+as2+as3+as4+as5)+.25*(hw1+hw2));}

    void Save(ofstream& outFile)

    {

        Students::save(outFile);

        outFile<<"Age:"<<mAge<<endl;

        outFile<<"Major:"<<mMajor<<endl;

        outFile<<"Grade:"<<mGrade2<<endl;

    }

    void printStats()

    {

        Students::printStats();

        cout<<"Age:"<<mAge<<endl;

        cout<<"Major:"<<mMajor<<endl;

        cout<<"Grade:"<<mGrade2<<endl;

    }

protected:
    int quiz1,quiz2,hw1,hw2,as1,as2,as3,as4,as5,project;
    int mAge;
    string mMajor;
    double mGrade2;

};





#endif

Now, let's get to it. This is the specific part of the code I have trouble with:

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
// Inserts the database entry generated directly into the program via ID location.
                if(database.size() < 1)
                {
                    database.push_back(new PM1(firstName,lastName,id,age,major,grade));
                    cout << "\nif runs";
                }
                else if (database.size() == 1)
                {
                    int i = 0;
                    if(database[i] -> getId() < id)
                        {
                            database.insert(database.begin() + 1, new PM1(firstName,lastName,id,age,major,grade));
                            cout << "\nelse if if == runs";
                        }
                    else
                        {
                            database.insert(database.begin(), new PM1(firstName,lastName,id,age,major,grade));
                            cout << "\nelse if else == runs";
                        }
                }
                else if (database.size() > 1)
                {
                    for(int i = 0; i < database.size(); i++)
                    {
                        if(database[i] -> getId() < id && database[i + 1] -> getId() >= id)
                        {
                            database.insert(database.begin() + i + 1, new PM1(firstName, lastName, id, age, major, grade));
                            cout << "\nelse if if runs";
                        }
                        else
                            {
                                database.push_back(new PM1(firstName,lastName,id,age,major,grade));
                                cout << "\nelse if else runs";
                            }
                    break;
                    }
                }
                else
                {

                cout << "\nDidn't work!";


                }


We basically need to create a system that allows us to insert the database entry directly into the vector from ID location only. So, the idea here is to not use the sort() function to sort the vector but instead push the new data entry directly into place.

First, you need to go to the first option's first option. Do not go to the first option's second option. As I've mentioned in my source code, I have not yet included the insertion code in that segment of the code just yet.

So when we run the program, let's go ahead and insert for the first value of the ID as 1. You can put down anything for the other values, they don't matter much. Be sure to do numerical values for the non-string variables (such as grade and age). Then you should see the new database entry inserted into the program.

Next, let's insert the value of the ID as 3. That also works fine.

Then let's insert the value of 2 for the ID. That also works just fine.

For the next value, we want to insert 5 and that also works great.

Then we insert 4 and it gets push_back'ed to the end of the list! I am not entirely sure what happened there. First thing I checked was whether it was because the code. But when I did the value of 2 after the value of 1 and 3, it inserted in the middle perfectly. It's only when we increase the vector size by greater than 3 that it truly poses a problem. For some reason, my "else if if" stops working.

That's not all, however. I believe this situation only occurs if the value to be inserted is less than the max by 1 (in terms of location). In other words, if we have a bigger database, and we insert for example in the middle of an earlier entry instead of the location at max-1, it works fine. Which is mind-boggling, at least to me.

I think I can fix this situation if I add another else if statement that involves a situation that covers the max-1 scenario when database size > 3. But, even though I could technically solve this issue and get an A on the assignment, I really do want to know if there's a better method. Because I feel like I made a mistake somewhere in my code and I'm just not spotting it.

I've rechecked this program several times and yet I have no solution. Sorry for the wall of text as well, guys. If you have any questions for me, please feel free to ask and I will answer the best of my ability. And thanks for having me.
I managed to figure out my problem. I had to set two more conditions that I originally missed. One was at location 0 and one was at location max and what to do when that occurred.
Topic archived. No new replies allowed.