Vectors

I have tried to run this program and every time that I do, it crashes. Do I have something wrong in my coding? Thank you for the help.

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

using namespace std;

vector <string> noun;
vector <string> verb;
vector <string> adjective;
vector <string> article;
vector <string> adverb;
vector <string> preposition;

string nounfile = "noun.txt";
string verbfile = "verb.txt";
string adjectivefile = "adjective.txt";
string articlefile = "article.txt";
string adverbfile = "adverb.txt";
string prepositionfile = "preposition.txt";

void readFromFile(string input, vector<string> &data)
{
    ifstream in(input.c_str()); // open input file;
    string str;
    while (in >> str)
    {
    data.push_back(str);
    }
}

string readRandomWord(vector<string> data)
{
    int pos = rand() % data.size(); // get random word from vector
    return data[pos];
}

string redSays()
{
    return readRandomWord(adjective) + " " + readRandomWord(noun);
}

string greenSays()
{
    return readRandomWord(article) + " " +readRandomWord(adjective) + " " + readRandomWord(noun) + " " +readRandomWord(verb);
}

string orangeSays()
{
    return readRandomWord(verb) + " or " +readRandomWord(verb);
}

string blueSays()
{
    return readRandomWord(verb) + " " +readRandomWord(adverb);
}

string yellowSays()
{
    return readRandomWord(noun) + " and " +readRandomWord(noun);
}

string purpleSays()
{
    return readRandomWord(preposition) + " " + readRandomWord(article) + " " +readRandomWord(adjective) + " " + readRandomWord(noun);
}

string redBluePurple()
{
    return redSays() + "\n" + blueSays() + "\n" + purpleSays() + "\n"; // combine different call according to function
}

string greenYellowPurpleOrange()
{
    return greenSays() + "\n" + yellowSays() + "\n" + purpleSays() + "\n" + orangeSays() + "\n"; // combine different call according to function
}

string greenYellowRedBlueOrangePurple()
{
    return greenSays() + "\n" + yellowSays() + "\n" + redSays() + "\n" + blueSays() + "\n" + orangeSays() + "\n" + purpleSays() + "\n";
}

string randomFour()
{
    vector<int> data;
    for (int i = 0; i < 4; i++) data.push_back(rand() % 6); // generate 4 random numbers from 0 to 5
    string result = "";
    for (int i = 0; i < data.size(); i++)
    {
    switch(data[i])
    { // call corresponding function
        case 0: result += yellowSays(); break;
        case 1: result += redSays(); break;
        case 2: result += blueSays(); break;
        case 3: result += orangeSays(); break;
        case 4: result += purpleSays(); break;
        case 5: result += greenSays(); break;
    }
        result += "\n";
    }
return result;
}

int main(int argc, char*argv[])
{
    srand(time(0));
    readFromFile(nounfile, noun); // read all files create vector
    readFromFile(adjectivefile, adjective);
    readFromFile(articlefile, article);
    readFromFile(verbfile, verb);
    readFromFile(adverbfile, adverb);
    readFromFile(prepositionfile, preposition);
    int choice = 1;

    while (choice)
    {
    cout << "(1) redBluePurple\n(2) greenYellowPurpleOrange\n(3) greenYellowRedBlueOrangePurple\n(4) randomFour\n(0) Quit" << endl;
    cin >> choice;
        switch (choice)
        {
        case 1: cout << redBluePurple() << endl; break;
        case 2: cout << greenYellowPurpleOrange() << endl; break;
        case 3: cout << greenYellowRedBlueOrangePurple() << endl; break;
        case 4: cout << randomFour() << endl; break;
        case 0: exit(0);
        }
    }
return 0;
}
Where are you checking to see if your vectors actually have anything in them?
I have some .txt files that I am using.
I have some .txt files that I am using.


No doubt.

Now go back and read what @cire wrote. Try changing your readRandomWord() routine to
1
2
3
4
5
6
string readRandomWord(vector<string> data)
{
    if ( data.size() == 0 ) cout << "This vector is EMPTY\n";
    int pos = rand() % data.size(); // get random word from vector
    return data[pos];
}
Or, to avoid accessing a non-existent element,
1
2
3
4
5
6
7
8
9
10
string readRandomWord(vector<string> data)
{
    if ( data.size() == 0 ) 
    {
        cout << "This vector is EMPTY\n";
        return  "***EMPTY***";
    }
    int pos = rand() % data.size(); // get random word from vector
    return data[pos];
}
@lastchance,
int pos = rand() % data.size(); // get random word from vector
if data.size is 0 you invoke undefined behaviour
Thanks @Chervil, @Thomas1965.

Yes, fair comment: I shouldn't have let it get to the % data.size() line at all in that circumstance. On my system it does at least put out the error message before crashing, so I thought that was sufficient to find the bug ... if lazy on my part, since I didn't allow the program to continue gracefully. I guess that's OK if, like me, you are running from the command line: maybe not so good from an IDE.

Best to go with @Chervil's suggestion, I think.

Maybe check that the input file stream opened as well.
Last edited on
Topic archived. No new replies allowed.