String array problem

Hello. So I am having a little problem right now. I am doing a list of Project ideas to improve my C++ skills ( just started ) and now the excercise is to make a random sentence generator. I jus twanted to make a simple one, without any grammer checks. I had the same code with char arrays for a Username generator ( worked perfectly ). The code below compiles but doesn't print anything. I don't get why it isn't returnign anything.

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
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

//The standard muster is : personal pronoun + verb + adjective + noun


// arrays
static const string pronoun[] = { "I", "You", "He", "She", "It", "We", "You", "They" };
static const string verb[] = {"like", "jump", "enjoy", "prefer", "chill", "works"};
static const string adjective[] = {"big", "tall", "small", "colorful"};
static const string noun[] = {"Trains", "Houses", "Apples", "Bananas", "Cities"};

// numbers for random
int pronounLength = sizeof(pronoun)-1;
int verbLength = sizeof(verb)-1;
int adjectiveLength = sizeof(adjective)-1;
int nounLength = sizeof(noun)-1;


string genPronoun()  // Random string generator function.
{
	return pronoun[rand() % pronounLength];
}

string genVerb()  // Random string generator function.
{
	return verb[rand() % verbLength];
}

string genAdjective()
{
	return adjective[rand() % adjectiveLength];
}

string genNoun()
{
	return noun[rand() % nounLength];
}

int main()
{
	srand(time(0)); // initialize random
	cout << genPronoun() << genVerb() << genAdjective() << genNoun();
	cout << endl;
	system("Pause");
}
First of all. If you're trying to improve your c++ Skills, dont use globals.

To add on what @Andy1992 said under me here. sizeof doesnt work like you think it works.

For example,

sizeof(pronoun)

This will return 224. Then when you try to

return pronoun[rand() % pronounLength];

It'll be like wooaaaaaah there buddy, thats way too much shit.
Last edited on
closed account (SECMoG1T)
The way you're deducing the array sizes is wrong, you'll certainly end up out of range , maybe you could take advantage of template to deduce the size for you...

1
2
3
4
5
6
7
8
9
10
11
12
13
template<unsigned int N>
unsigned int get_size(const string (&array)[N])
{
    return N-1;
}


///in usage just call the function with an array argument

int pronounLength = get_size(pronoun);
int verbLength = get_size(verb);
int adjectiveLength = get_size(adjective);
int nounLength = get_size(noun);
Last edited on
What are you expecting sizeof(<string-array>) to return?
It's not going to return the number of elements in the array.

Better approach is to define the ints before the string arrays and then use those constants in both the arrays declarations and in the corresponding function:

1
2
3
4
 
const int pronounLength = 8;

static const string pronoun[pronounLength] = { "I", "You", "He", "She", "It", "We", "You", "They" };




@Andy1992. Please, when someone says they just started c++, and you see them using globals like there's no tomorrow. Dont introduce templates to them, or anything relatively complicated like the code you gave.

Help them instead with the tools they've shown you that they know. <3
Last edited on
closed account (SECMoG1T)
@TraikNeaj my bad XD... but still i meant no harm for the Op's skills ,anyway thanks buddy for the reminding me to be considerate.
@andy1992 I will ignore the fact that you spelled my name wrong... for this time :D

Yeh man. Ive actually seen a couple of times where you've done this but never said anything until now, its like. OP is having troubles understanding for-loops and then you suggest he uses linked lists and OpenGL. (Totally just made that up but you get the idea xDD)

Thanks to all of you! @TarikNeaj I do understand for loops and I am currently moving from Delphi to C++, the code I wrote was the same with char arrays which worked. That's what I just realized. I only knew sizeof() and for some reason i meesed it up. Works now! :)
I didnt mean OP as in you, look at the last part of my post.

(Totally just made that up but you get the idea xDD)
Topic archived. No new replies allowed.