Vector not being cleared

I havent done any programming in a long time and i'm a bit rusty, but I'm trying to set up a name generator class, still in the early phase so no name generating yet, but trying to output contents of a file so im loading it into a vector, but it wont seem to clear, I have a function made to clear it but its not working, can anyone tell me why?

I get no compiler errors or warnings.



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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class NameGenerator
{
    public:
        NameGenerator(int nameLength = 0, string name = "Default"): nameLength(nameLength),
                                                                    name(name)
        {
            nameList.reserve(1);
            nameList.push_back(name);
        }

    private:
        int nameLength;
        string name;
        vector<string> nameList;

    public:
        void GenerateName();
        void DisplayNames();


    private:
        void setNameLength(int n)
        { name = n; }

        int getNameLength()
        { return name.length(); }

        void ClearNameListVector();

        void LoadNames();
};

void NameGenerator::GenerateName()
{

}

void NameGenerator::LoadNames()
{
    NameGenerator NG;

    ifstream loadNameList("Names.txt");

    while(getline(cin, name, '\n'))
    {
        loadNameList >> NG.name;
        NG.nameList.push_back(name);
    }
}

void NameGenerator::ClearNameListVector()
{
    NameGenerator NG;

    NG.nameList.clear();
    name.clear();
}

void NameGenerator::DisplayNames()
{
    NameGenerator NG;

    NG.ClearNameListVector();

    for(unsigned int i = 0; i < NG.nameList.size(); i++)
    {
        cout << nameList[i] << endl;
    }
}

int main()
{
    NameGenerator NG;

    NG.DisplayNames();

    return 0;
}
Line 62 clears the nameList of local object NG(1) that was created on line 60.

Line 63 clears name of object NG(2) that was created on line 70.

Line 74 reads a value from nameList that belongs to object NG(3) created on line 80.


Three separate NameGenerator objects. That is not one short -- that is two too many.
Why do you create these temporary local variables NG in NameGenerator::DisplayNames() and NameGenerator::LoadNames() ? Why don't you use vector<string> nameList; instead ?
I thought thats what I was supposed to do, but now that I look at it, since its already inside of the class function, NameGenerator NG wasnt needed since It can already access all that stuff, forgot about that, so I have this now, but it still doesnt erase the contents fo vector, it still outputs as "Default"

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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class NameGenerator
{
    public:
        NameGenerator(int nameLength = 0, string name = "Default"): nameLength(nameLength),
                                                                    name(name)
        {
            nameList.reserve(1);
            nameList.push_back(name);
        }

    private:
        int nameLength;
        string name;
        vector<string> nameList;

    public:
        void GenerateName();
        void DisplayNames();


    private:
        void setNameLength(int n)
        { name = n; }

        int getNameLength()
        { return name.length(); }

        void ClearNameListVector();

        void LoadNames();
};

void NameGenerator::GenerateName()
{

}

void NameGenerator::LoadNames()
{
    ifstream loadNameList("Names.txt");

    ClearNameListVector();

    while(getline(loadNameList, name, '\n'))
    {
        loadNameList >> name;
        nameList.push_back(name);
    }
}

void NameGenerator::ClearNameListVector()
{
    nameList.clear();
    name.clear();
}

void NameGenerator::DisplayNames()
{
    for(unsigned int i = 0; i < nameList.size(); i++)
    {
        cout << nameList[i] << endl;
    }
}

int main()
{
    NameGenerator NG;

    NG.DisplayNames();

    return 0;
}
Last edited on
Line 19: Why is namelength a member variable? Neither your getter nor setter use it. You do reference it in your constructor, but to what point I do not see since a std::string always tracks its own length.

Line 20: Why is name a member variable? It should be a local variable where needed.

Line 29-30: setNameLength makes no sense. You're trying to assign a int to a string.

Line 46: name should be a local variable here.

Line 61: Not needed if name is not a member variable.

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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class NameGenerator
{   vector<string> nameList;

public:
    NameGenerator (const string & name = "Default") 
    {   nameList.reserve(1);
        nameList.push_back(name);
    }

    void GenerateName();
    void DisplayNames() const;

private:
        void ClearNameListVector();
        void LoadNames();
};

void NameGenerator::GenerateName()
{}

void NameGenerator::LoadNames()
{   string      name;
    ifstream loadNameList("Names.txt");

    ClearNameListVector();
    while(getline(loadNameList, name, '\n'))
    {   loadNameList >> name;
        nameList.push_back(name);
    }
}

void NameGenerator::ClearNameListVector()
{   nameList.clear();
}

void NameGenerator::DisplayNames() const
{   for (unsigned int i = 0; i < nameList.size(); i++)
    {   cout << nameList[i] << endl;
    }
}

int main()
{   NameGenerator NG;

    NG.DisplayNames();
    system ("pause");
    return 0;
}

Last edited on
Alright I got it working thanks.
Topic archived. No new replies allowed.