我是C ++的新手,我正在做一个小项目,我必须通过使用数组中的字符串来生成一个句子。问题是,即使计算从一个数组移动到下一个数组,或者有时完全跳过该数组,有时来自同一数组的字符串也会运行两次。以下是代码打印的一些示例:
some cat ran drove the shop. //here the verb[] array is ran twice
some girl town on a boy. //here the nounPerson[] array is run but immediately the nounPlace[] array is ran next, instead of verb[] array like I tried to do in the code
the the ran over the boy //here the same thing happens, the first word is printed twice and the nounPerson[] is skipped
也许我对数组的理解是错误的,或者也许是随机数生成器。这是我的代码
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
string article[5] = {"the ", "a ", "one ", "some ", "my "};
string nounPerson[5] = {"boy ", "girl ", "dog ", "cat ", "person "};
string nounPlace[5] = {"town ", "cafe ", "shop ", "store ", "house "};
string verb[5] = {"drove ", "jumped ", "ran ", "walked ", "striked "};
string preposition[5] = {"to ", "from ", "over ", "across ", "on "};
string sentence;
int word, word1, word2, word3, word4, word5;
srand(time(0));
word = rand() % 6;
word1 = rand() % 6;
word2 = rand() % 6;
word3 = rand() % 6;
word4 = rand() % 6;
word5 = rand() % 6;
//this is where the strings from the arrays add up to make a sentence
sentence = article[word];
sentence = sentence + nounPerson[word1];
sentence = sentence + verb[word2];
sentence = sentence + preposition[word3];
sentence = sentence + article[word4];
sentence = sentence + nounPlace[word5];
cout << sentence << endl;
}
有时我也会收到std :: bad_alloc错误而且我不知道那是什么/如何修复
答案 0 :(得分:2)
rand() % 6
将生成0
和5
之间的值,包括两者。其中,所有值都是除5
之外的数组的有效索引。获得5
后,使用越界索引访问数组,这会导致未定义的行为。
改为使用rand() % 5
。
word = rand() % 5;
word1 = rand() % 5;
word2 = rand() % 5;
word3 = rand() % 5;
word4 = rand() % 5;
word5 = rand() % 5;
我会使用index
而不是word
,因为它们是数组的索引。
index1 = rand() % 5;
index2 = rand() % 5;
index3 = rand() % 5;
index4 = rand() % 5;
index5 = rand() % 5;
index6 = rand() % 5;
答案 1 :(得分:1)
您的代码中有一个非常小的更正:
srand(time(0));
word = rand() % 5;
word1 = rand() % 5;
word2 = rand() % 5;
word3 = rand() % 5;
word4 = rand() % 5;
word5 = rand() % 5;
由于数组的索引从0开始,因此大小为5的数组从0开始,到4结束。 并且您在rand()函数中生成0到5(包括两者)之间的数字。因此,只需将其从6更改为5即可生成0到4(包括两者)。
答案 2 :(得分:0)
你的数组一个接一个地放在堆栈上。它们中的每一个长度为5,因此最大允许指数为4.但除以6 - % 6
,您只需要除以5.
而不是c-arrays,您可以使用std::array
并使用at
来检查元素:
const int size = 5;
array<string,size> article = { "the ", "a ", "one ", "some ", "my " };
array<string, size> nounPerson = { "boy ", "girl ", "dog ", "cat ", "person " };
array<string, size> nounPlace = { "town ", "cafe ", "shop ", "store ", "house " };
array<string, size> verb = { "drove ", "jumped ", "ran ", "walked ", "striked " };
array<string, size> preposition = { "to ", "from ", "over ", "across ", "on " };
string sentence;
int word, word1, word2, word3, word4, word5;
srand(time(0));
// the best is to divide by array size,
// in case of c-arrays it would be % (sizeof(article)/sizeof(string))
word = rand() % article.size();
word1 = rand() % nounPerson.size();
word2 = rand() % verb.size();
word3 = rand() % preposition.size();
word4 = rand() % article.size();
word5 = rand() % nounPlace.size();
//this is where the strings from the arrays add up to make a sentence
sentence = article[word];
sentence = sentence + nounPerson.at(word1);
sentence = sentence + verb.at(word2);
sentence = sentence + preposition.at(word3);
sentence = sentence + article.at(word4);
sentence = sentence + nounPlace.at(word5);
cout << sentence << endl;