Vector help

Ok so in my program i have a vector that outputs the inmate names, its supposed to output 10 or 11 random names but there all the same, why? i dont get compiler errors but if you run it youll see, just choose load, then 1 to virew inmate list, it gives the same exact name 10 times. how do i fix it?

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

using namespace std;

struct Vars
{
    void Game();
    string randNames();
    string randCrimes();

    long long int money;
    int prisoners;
    string playerName;
    string prisonName;
};

int main()
{
    int choice;

    Vars v;

    cout << "1) New" << endl;
    cout << "2) Load\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        v.money = 50000;
        v.prisoners = 0;

        ofstream file;
        file.open("prison.txt");

        cin.ignore(1000, '\n');

        cout << "Hello please enter your name" << endl;
        getline(cin, v.playerName);
        file << v.playerName << endl;

        cout << "\n";

        cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
        getline(cin, v.prisonName);
        file << v.prisonName << endl;

        cout << "\n";

        cout << "Ok thank you lets start the game" << endl;
        cin.get();

        file << v.money << endl;
        file << v.prisoners << endl;

        file.close();

        v.Game();
    }
    else if(choice == 2)
    {
        ifstream file;

        file.open("prison.txt");

        file >> v.playerName;
        file >> v.prisonName;
        file >> v.money;
        file >> v.prisoners;

        file.close();

        v.Game();
    }
}

void Vars::Game()
{
    int choice;
    string vect;

    cout << "Main Menu\n" << endl;

    cout << "What do you want to do?\n" << endl;

    cout << "1) View list of prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison funds and spending" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            cout << "Inmate list\n" << endl;

            vector<string> Svect;

            for(int i = 0; i < 11; i++)
            {
                Svect.push_back(randNames());
            }

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

}

string Vars::randNames()
{

    int ran;

    string names[7] =
    {
        "Jerry",
        "Mike",
        "Phil",
        "Robert",
        "Allen",
        "Alex",
        "Tim"
    };

    srand(time(0x0));

    for(int i = 0; i < 7; i++)
    {
        ran = rand() % 7;
    }

    return names[rand() % 7];
}

string randCrimes()
{
    int ran;

    string names[7] =
    {
        "Rape",
        "Child Abuse",
        "Murder",
        "Assault",
        "Breaking and Entering",
        "Domestic Violence",
        "Unlawful Posession of a Firearm"
    };

    srand(time(0x0));

    for(int i = 0; i < 7; i++)
    {
        ran = rand() % 7;
    }

    return names[rand() % 7];
}
Last edited on
How about you show us your code.

Edit: Line 16 you have:
 
long long int money;


Edit2:
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
string Vars::randNames()
{

    int ran;

    string names[7] =
    {
        "Jerry",
        "Mike",
        "Phil",
        "Robert",
        "Allen",
        "Alex",
        "Tim"
    };

    srand(time(0x0));

    for(int i = 0; i < 7; i++) // What is the purpose of this loop or the ran variable?
    {
        ran = rand() % 7;
    }

    return names[rand() % 7];
}
Last edited on
Oh that was just left over from something i was doing.
Updated code:

The vector keep outputting the same name and offense 11 times when it should output different names and offenses, what do 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

struct Vars
{
    void Game();
    string randNames();
    string randCrimes();

    long int money;
    int prisoners;
    string playerName;
    string prisonName;
};

int main()
{
    int choice;

    Vars v;

    cout << "1) New" << endl;
    cout << "2) Load\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        ofstream file;
        file.open("prison.txt");

        cin.ignore(1000, '\n');

        cout << "Hello please enter your name" << endl;
        getline(cin, v.playerName);
        file << v.playerName << endl;

        cout << "\n";

        cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
        getline(cin, v.prisonName);
        file << v.prisonName << endl;

        cout << "\n";

        cout << "Ok thank you lets start the game" << endl;
        cin.get();

        v.money = 50000;
        v.prisoners = 0;

        file << v.money << endl;
        file << v.prisoners << endl;

        file.close();

        v.Game();
    }
    else if(choice == 2)
    {
        ifstream file;

        file.open("prison.txt");

        file >> v.playerName;
        file >> v.prisonName;
        file >> v.money;
        file >> v.prisoners;

        file.close();

        v.Game();
    }
}

void Vars::Game()
{
    int choice;
    string vect;

    cout << "Main Menu\n" << endl;

    cout << "What do you want to do?\n" << endl;

    cout << "1) View list of prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            cout << "Inmate list, maximum 25 inmates\n" << endl;

            vector<string> nameVect;
            vector<string> crimeVect;

            for(int i = 0; i < 11; i++)
            {
                nameVect.push_back(randNames());
                crimeVect.push_back(randCrimes());
            }

            cout << "Name " << "Crime\n" << endl;

            for(int i = 0; i < 11; i++)
            {
               cout << nameVect[i] << " ";
               cout << crimeVect[i] << endl;
            }
    }
}

string Vars::randNames()
{
    string names[17] =
    {
        "Jerry",
        "Mike",
        "Phill",
        "Robert",
        "Allen",
        "Alex",
        "Tim",
        "Steven",
        "Ronald",
        "David",
        "Harold",
        "Thomas",
        "Andrew",
        "Carlito",
        "Frank",
        "Dale",
        "John"
    };

    srand(time(0x0));

    return names[rand() % 17];
}

string Vars::randCrimes()
{
    string crimes[9] =
    {
        "Rape",
        "Child Abuse",
        "Murder",
        "Assault",
        "Breaking and Entering",
        "Domestic Violence",
        "Unlawful Posession of a Firearm",
        "Destruction of private property",
        "Kidnapping"
    };

    srand(time(0x0));

    return crimes[rand() % 9];
}
Last edited on
bump
Topic archived. No new replies allowed.