Vector Help

I havent been programming for quite some time but decided to come back to it. I wanted to start with some arrays and vector stuff, this is what i have.


The problem is that with the Vector, when i output the contents to the file there are spaces in the document, it corresponds to the amount of elements the user enters, so if they enter 5 elements, there will be 5 blank lines in the element and then the info. I was able to fix 3 problems with it that i had before but I cant seem to figure this one out, however I have a feeling that the number of elements that the user enters is being pushed back into the vector and when they enter some stuff to put in, if they entered 3, then the stuff they entered will be entered after the 3rd element, so what can i do?

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

using namespace std;

void SetArray(string str_array[], int array_size);
void PrintArray(string str_array[], int array_size);
void SaveArray(string str_array[], int array_size);
void SetVector(vector<string>& str_vect, int vector_size);
void PrintVector(vector<string>& str_vect, int vector_size);
void SaveVector(vector<string>& str_vect, int vector_size);
inline void Ignore();

int main()
{

    /*
    cout << "How many elements will this array have?" << endl;
    cout << "#";

    int array_size;

    cin >> array_size;

    Ignore();

    string str_array[array_size];

    SetArray(str_array, array_size);
    PrintArray(str_array, array_size);
    SaveArray(str_array, array_size);
    */

    cout << "How many elements will the vector have?" << endl;
    cout << "#";

    int vector_size;

    cin >> vector_size;

    Ignore();

    vector<string> str_vect(vector_size);

    SetVector(str_vect, vector_size);
    PrintVector(str_vect, vector_size);
    SaveVector(str_vect, vector_size);

    return 0;
}

void SetArray(string str_array[], int array_size)
{
    int elementNumber = 0;

    for(int i = 0; i < array_size; i++)
    {
        cout << "Enter string for element #" << ++elementNumber << ": ";
        getline(cin, str_array[i]);
    }
}

void PrintArray(string str_array[], int array_size)
{
    cout << endl;

    for(int i = 0; i < array_size; i++)
    {
        cout << str_array[i] << endl;
    }
}


void SaveArray(string str_array[], int array_size)
{
    ofstream save;

    save.open("SaveFile.txt");

    for(int i = 0; i < array_size; i++)
    {
        save << str_array[i] << endl;
    }
}


void SetVector(vector<string>& str_vect, int vector_size)
{
    int elementNumber = 0;
    string elementString;

    for(int i = 0; i < str_vect.size(); i++)
    {
        if(elementNumber == vector_size)
        {
            break;
        }

        cout << "Enter information into vector for element #" << ++elementNumber << ": ";
        getline(cin, elementString);

        str_vect.push_back(elementString);
    }
}

void PrintVector(vector<string>& str_vect, int vector_size)
{
    for(int j = 0; j < str_vect.size(); j++)
    {
        cout << str_vect[j] << endl;
    }
}

void SaveVector(vector<string>& str_vect, int vector_size)
{
    ofstream saveVector;

    saveVector.open("Vector Info.txt");

    cout << "\nVector info saved..." << endl;

    for(int sv = 0; sv < str_vect.size(); sv++)
    {
        saveVector << str_vect[sv] << endl;
    }
}

inline void Ignore()
{
    cin.ignore();
}
Line 45 sets N default elements in your vector at construction.
Line 104 appends N elements to your vector.

BTW, you don't need to pass the vector_size to any of the functions, since you can query the length of the referenced vector.

Also, you don't need lines 91 and 96 through 99.
On line 101, replace ++elementNumber with (i+1).

Hope this helps.
ah, thank you, I made the changes and did this and it worked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void SetVector(vector<string>& str_vect)
{
    string elementString;

    for(int i = 0; i <= str_vect.size(); i++)
    {
        if(elementString == "-1")
        {
            break;
        }
        cout << "Enter information into vector for element #" << i + 1 << ": ";
        getline(cin, elementString);

        str_vect.push_back(elementString);
    }

    str_vect.pop_back();
}
The loop condition i <= str_vect.size() is never met, so it is useless. If you want you could remove the if statement and use elementString != "-1" as loop condition instead.
Last edited on
So now your program doesn't ask how many elements?


Personally, I prefer to avoid asking things like "how many?" and just let the user's input figure that out:

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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void SetVector(vector<string>& str_vect)
{
    cout << "Enter each vector element. Press Enter twice to finish.\n";
    int i = 0;
    while (++i)
    {
        cout << i << ": ";
        string elementString;
        getline(cin, elementString);

        if (elementString.empty()) break;

        str_vect.push_back(elementString);
    }
}

int main()
{
    vector<string> str_vect;
    SetVector(str_vect);
    cout << "\nstr_vect:\n";
    for (auto s : str_vect)
        cout << s << "\n";
}

Hope this helps.
"So now your program doesn't ask how many elements?"

That's correct, I just let the user input however many they want and then when they enter -1 it exits the loop and pops back 1 to remove the -1 from the element list. I like your code better though, so if its empty it will just exit and not put anything else in the list, then i dont even have to enter -1 or pop back anything.
Last edited on
Topic archived. No new replies allowed.