Choosing a random number gives me error in array

My code
[CODE]
#pragma once

#include <iostream>
#include <cstdlib>
#include<ctime>

class Sentence
{
private:
char *articles[5];
char *noun[5];
char *verb[5];
char *preposition[5];
public:
Sentence();
~Sentence();
std::string CreateSentence();
};

Sentence::Sentence()
{
articles[0] = "the";
articles[1] = "a";
articles[2] = "one";
articles[3] = "some";
articles[4] = "any";
noun[0] = "boy";
noun[1] = "girl";
noun[2] = "dog";
noun[3] = "town";
noun[4] = "car";
verb[0] = "drove";
verb[1] = "jumped";
verb[2] = "ran";
verb[3] = "walked";
verb[4] = "skipped";
preposition[0] = "to";
preposition[1] = "from";
preposition[2] = "over";
preposition[3] = "under";
preposition[4] = "on";
}

std::string Sentence::CreateSentence()
{
std::srand(std::time(0));
int r1 = rand() % 6;
int r2 = rand() % 6;
int r3 = rand() % 6;
int r4 = rand() % 6;
int r5 = rand() % 6;
int r6 = rand() % 6;

std::string sentence = articles[r1] + noun[r2] + verb[r3] + preposition[r4] + articles[r5] + noun[r6];
return sentence;
}
[/code]

I get the error "expression must have integral or unscoped enum type" when I try to add more than one word to the string. Ideas?
If you know what std::string is, why then do you opt to use char * in your code? You should know that char* and std::string are NOT the same and as such most operations on std::string cannot be applied to char*. All pointers types only support operator+ together with an integer, so the integer will act as an index into some random positions in memory where the pointer points to. std::string, OTOH, "librarily" supports operator+ with char*, std::string and some other types.

I'd assume you didn't know about std::vector already, so I won't bother suggesting you use it. In that case, you can simply change your code like so:

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

class Sentence
{
private:
std::string articles[5];
std::string noun[5];
std::string verb[5];
std::string preposition[5];
public:
Sentence();
~Sentence();
std::string CreateSentence();
};

Sentence::~Sentence() {}

Sentence::Sentence()
{
articles[0] = "the";
articles[1] = "a";
articles[2] = "one";
articles[3] = "some";
articles[4] = "any";
noun[0] = "boy";
noun[1] = "girl";
noun[2] = "dog";
noun[3] = "town";
noun[4] = "car";
verb[0] = "drove";
verb[1] = "jumped";
verb[2] = "ran";
verb[3] = "walked";
verb[4] = "skipped";
preposition[0] = "to";
preposition[1] = "from";
preposition[2] = "over";
preposition[3] = "under";
preposition[4] = "on";
}

std::string Sentence::CreateSentence()
{
std::srand(std::time(0));
int r1 = rand() % 6;
int r2 = rand() % 6;
int r3 = rand() % 6;
int r4 = rand() % 6;
int r5 = rand() % 6;
int r6 = rand() % 6;

std::string sentence = articles[r1] + " " + noun[r2] + " " + verb[r3] + " " + preposition[r4] + " " + articles[r5] + " " + noun[r6];
return sentence;
}
int r1 = rand() % 6;
This will return a value from 0 to 5, inclusive. When you try to do
 
articles[r1]

when r1 == 5, you will be accessing memory outside of your array. As a result undefined behaviour will ensue.
Topic archived. No new replies allowed.