Randomly Generated number problem.

I'm still getting in to programing in C++, but one of the things I wanted to do for fun is write a program that randomly generates a number and then compares that number to a numbered list of stringed characters. Once it finds the corresponding number in the list it displays that string of characters. I know the idea of using randomly generated number using random(time(NULL)), but my problem is then taking that number and comparing it to a numbered list and displaying the string attached to the corresponding number. If someone could point me in the right direction as to how to compare them I would be grateful.
Can you show an example what are you trying to achieve?
Last edited on
The worst part is I haven't found an example, in code at least. A non-code related example would be like a random loot generator. I thought about using switch cases to achieve the list or even pulling from another function that defines a list of strings. I'm just not sure how to go about it.
So you need something like
Generate random number from 0 to 2
Display string corresponding to number:
0 → Hello
1 → Bye
2 → What do you want?

Is it correct?
Yes! I believe that is it! I don't need the blatant code I just need a nudge in the right direction about how it all comes together.
1) use an array (or vector, or whatever sequential container you like)
1
2
3
4
5
6
7
8
const int ARRSIZE = 3;
const char* descriptions[ARRSIZE] = {"Hello", "Bye", "What do you want?"};
int index = rand() % ARRSIZE;
std::cout << descriptions[index];
//or
const std::vector<std::string> descriptions = {"Hello", "Bye", "What do you want?"};
int index = rand() % descriptions.size();
std::cout << descriptions[index];


2) use std::map which would be useful if your indexes are not in range 0..n
This will allow to do something like
1
2
std::cout << desc_map["Boots"];
These are boots
Last edited on
Thank you for the advice and the help. It's very much appreciated.
Topic archived. No new replies allowed.