Concatenating array of chair pointers to form sentences - Segment Faults

So I am writing a program, which I have been instructed to use arrays of pointers to chars and cannot use strings, which would make my life a lot easier. Anyways, I am to use random numbers to decide which words to pick out from the array of chars and place into the sentence. I am then supposed to concatenate the words into a sentence and output it. However, my program seems to segment fault because of the way I am trying to use the arrays of pointers,and since I am relatively new to pointers I am unsure of where I went wrong.

Thanks for the assistance.
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
  #include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
const int max_for_article = 3;
const int max_for_others = 9;
void generate_values_article(int word_selector_value);
void generate_values_other(int word_selector_value);

int main()
{
	string sentence;
	int temp_for_Article_1;
	int temp_for_Article_2;
	int temp_for_nounValue;
	int temp_for_noun_2;
	int temp_for_verbValue;
	int temp_for_prepValue;
	const char *Article[max_for_article] = { "A", "an", "the" };
	const char *noun[max_for_others] = { "man", "woman", "dog", "cat", "President", "tree", "car", "artist", "programmer", };
	const char *verb[max_for_others] = { "ask", "analyze", "attempt", "brought", "bought", "billed", "caught", "concurred", };
	const char *preposition[max_for_others] = { "aboard", "around", "at", "behind", "beneath", "beyond", "considering", "concerning", "for", };
	int value_for_article;
	int value_for_others;

	generate_values_article(value_for_article); 
	temp_for_Article_1 = value_for_article; //Assigning value to extract article

	generate_values_article(value_for_article);
	temp_for_Article_2 = value_for_article; //Assigning value to extract second article

	generate_values_other(value_for_others); 
	temp_for_nounValue = value_for_others; //Assigning value to extract noun

	generate_values_other(value_for_others);
	temp_for_noun_2 = value_for_others; //Assigning value to extract second noun

	generate_values_other(value_for_others);
	temp_for_verbValue = value_for_others; //Assigning value to extract verb

	generate_values_other(value_for_others);
	temp_for_prepValue = value_for_others; //Assigning value to extract prep



	sentence = string((*Article[temp_for_Article_1]) + " " + *noun[temp_for_nounValue]) + " " + *verb[temp_for_verbValue] + " " + *preposition[temp_for_prepValue] + " " + *Article[temp_for_Article_2] + " " + *noun[temp_for_noun_2] + ".";
	cout << sentence;
	



	return 0;
}

void generate_values_article(int word_selector_value)
{
	srand(time(NULL));
	word_selector_value = rand() % 3;
}
void generate_values_other(int word_selector_value)
{
	word_selector_value = rand() % 9;
}
Your temp variables have garbage values since your generate functions only modify a copy of the argument fed to them.

You are familiar with passing by reference?
Topic archived. No new replies allowed.