Hey you guys i have a major problem here basically im a begginer c++ program student and i am trying to get use to vectors and functions so i made this program like a mad lib game or a bear hunt as you say. The program basically have vectors (duh) were the user inputs five verbs, nouns, items, etc. into the vectors then the vectors would then put the story i made in random places but i don't know how to random my vectors please help me :D Here is my code:

// Bear Hunt.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h" 
#include <iostream> 
#include <vector> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
#include <algorithm> 
 
using namespace std; 
 
 
 
void Welcome(); 
 
int menu(); 
 
void Genova(); 
 
void Menu(); 
 
void Jebusland(); 
 
int main() 
 
{

cout <<  "\t\t\Welcome to The Pendragon Bear Hunt!! \n\n"; 
 
string item;
string verb;
string noun;
 
 
int count = 0; 
 
Welcome();
while
(count != 3) 
{
count = menu();
 
if
(count == 1) 
{
Genova();
}
else
if(count == 2) 
{
Jebusland();
}
}
return
0; 
}
 
void Welcome() 
{
int
number; 
cout << 
"....Beware!! the planet of Jenova!!!\n\n\n"; 
cout << 
"Please follow the instructions to countiue.."; 
 
cout << 
"What planet would you like to go to\n"; 
cout << 
"1.Genova\n"; 
cout << 
"2.Jebus Land \n"; 
cin >> number;
 
if
(number == 1) 
{
Genova();
}
else
if(number == 2) 
{
Jebusland();
}
return
; 
}
 
void Genova() 
{
string item;
string verb;
string noun;
 
vector<string>items;
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five items: \n\n"; 
cin >> item;
items.push_back(item);
}
srand(time(0));
random_shuffle(item.begin(), item.end());
vector<string>verbs;
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five verbs: \n\n"; 
cin >> verb;
items.push_back(verb);
}
 
vector<string>nouns;
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five nouns: \n\n"; 
cin >> noun;
items.push_back(noun);
}
cout << item;
cout << "now the leader of all of the travelers are now establishing a new planet\n\n"; 
cout << "Inside of our Solar System located were Second Earth would be."; 
cout << item;
cout << "To go with the idea but it wasnt a easy debate. Many other travelers wanted it to be part of the Veelox. Most of the people decided to they wanted it by\n\n"; 
cout << "Cloral for reasonable economical reasons. But Bobby decided to change to change the location from Second Earth\n\n"; 
cout << "To Dendourn his first area were he started traveling at his age. It was perfect for him he loved that idea. But\n\n"; 
cout << "What is the name of the planet and for what purpose is it being created.\n\n"; 
cout << "Bobby called it 'Genova' the planet of source\n\n"; 
cout << "Bobby has discoverd that he can fuse materia with the inner core of other planets to make a unique source"; 
cout << "He wanted to insert the materia with a star and produce a Big Bam Theory sort of use to it"; 
cout << "Then he would send special-ops travelers that would search the planet and discover any interference with the planets production like"; 
cout << "Quigs, Animals, or Beings of sorts that have been inhabited inside the planet"; 
}
 
void Jebusland() 
{
 
	string item;
string verb;
string noun;
 
vector<string>items;
for
(int i = 0; i < 5; ++i) 
{
srand(time(0));
cout << "Type in five items: \n\n"; 
cin >> item;
items.push_back(item);
}


for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five verbs: \n\n"; 
cin >> verb;
items.push_back(verb);
}
 
vector<string>nouns;
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five nouns: \n\n"; 
cin >> noun;
items.push_back(noun);
}
cout << "Jebusland was a planet of peace until a vessel landed"; 
cout << item;
cout << "Inside of that vessel lived hordes of bears from there planet second earth";
cout << item;
cout << "What the travelers didnt know that the bear race was full of highly intelligent bears that surpass any human being and were far ahead in technology"; 
}
 
int
menu() 
{
int
choice; 
cout << "\n\nWhat planet would you like to go to\n"; 
cout << "\n1. Go to Genova \n"; 
cout << "2. Go to Jebusland \n"; 
cout << "3. Quit \n"; 
cin >> choice;
return
choice; 
}

Recommended Answers

All 7 Replies

You could use this fuction to get random number

int rand_gen(int lowest, int higest){
    int random_integer;
    int range=(higest-lowest)+1;
    for(int index=0; index<20; index++){
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    }
    return random_integer;
}

And then when u want to get random number you just call function like
rand_gen(1,7); - this will return random number beetwen 1 and 7, since u have vector with five items i suppose u should use from 0,4.
And you should declare vectors outside functions if u want to use the in other functions.

And when u want to call random item from vector you just write

items.at(rand_gen(0,4));

I hope that helps a little

Here is code inserted in your c++ program, i just moved vectors from outside functions, and used rand_gen function to generate random number betweeen 0-4 and you get random word.

// Bear Hunt.cpp : Defines the entry point for the console application.
//
 
#include <iostream> 
#include <vector> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
#include <algorithm> 
 
using namespace std; 
string item;
string verb;
string noun;
vector<string>items;
vector<string>verbs;
vector<string>nouns;
  
void Welcome(); 
int menu(); 
void Genova(); 
void Menu();
int rand_gen(int lowest, int higest);
void Jebusland(); 
int main() 
 
{

cout <<  "\t\t\Welcome to The Pendragon Bear Hunt!! \n\n"; 
 
 
 
int count = 0; 
 
Welcome();
while
(count != 3) 
{
count = menu();
 
if
(count == 1) 
{
Genova();
}
else
if(count == 2) 
{
Jebusland();
}
}
return
0; 
}
 
void Welcome() 
{
int
number; 
cout << 
"....Beware!! the planet of Jenova!!!\n\n\n"; 
cout << 
"Please follow the instructions to countiue.."; 
 
cout << 
"What planet would you like to go to\n"; 
cout << 
"1.Genova\n"; 
cout << 
"2.Jebus Land \n"; 
cin >> number;
 
if
(number == 1) 
{
Genova();
}
else
if(number == 2) 
{
Jebusland();
}
return
; 
}
int rand_gen(int lowest, int higest){
    int random_integer;
    int range=(higest-lowest)+1;
    for(int index=0; index<20; index++){
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    }
    return random_integer;
}
void Genova() 
{
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five items: \n\n"; 
cin >> item;
items.push_back(item);
}
srand(time(0));
random_shuffle(item.begin(), item.end());

for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five verbs: \n\n"; 
cin >> verb;
items.push_back(verb);
}
 
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five nouns: \n\n"; 
cin >> noun;
items.push_back(noun);
}
cout << items.at(rand_gen(0,4))<<" ";
cout << "now the leader of all of the travelers are now establishing a new planet\n\n"; 
cout << "Inside of our Solar System located were Second Earth would be."; 
cout << items.at(rand_gen(0,4)) <<" ";
cout << "To go with the idea but it wasnt a easy debate. Many other travelers wanted it to be part of the Veelox. Most of the people decided to they wanted it by\n\n"; 
cout << "Cloral for reasonable economical reasons. But Bobby decided to change to change the location from Second Earth\n\n"; 
cout << "To Dendourn his first area were he started traveling at his age. It was perfect for him he loved that idea. But\n\n"; 
cout << "What is the name of the planet and for what purpose is it being created.\n\n"; 
cout << "Bobby called it 'Genova' the planet of source\n\n"; 
cout << "Bobby has discoverd that he can fuse materia with the inner core of other planets to make a unique source"; 
cout << "He wanted to insert the materia with a star and produce a Big Bam Theory sort of use to it"; 
cout << "Then he would send special-ops travelers that would search the planet and discover any interference with the planets production like"; 
cout << "Quigs, Animals, or Beings of sorts that have been inhabited inside the planet"; 
}
 
void Jebusland() 
{
 

for
(int i = 0; i < 5; ++i) 
{
srand(time(0));
cout << "Type in five items: \n\n"; 
cin >> item;
items.push_back(item);
}


for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five verbs: \n\n"; 
cin >> verb;
items.push_back(verb);
}
 
for
(int i = 0; i < 5; ++i) 
{
cout << 
"Type in five nouns: \n\n"; 
cin >> noun;
items.push_back(noun);
}
cout << "Jebusland was a planet of peace until a vessel landed"; 
cout << item;
cout << "Inside of that vessel lived hordes of bears from there planet second earth";
cout << item;
cout << "What the travelers didnt know that the bear race was full of highly intelligent bears that surpass any human being and were far ahead in technology"; 
}
 
int
menu() 
{
int
choice; 
cout << "\n\nWhat planet would you like to go to\n"; 
cout << "\n1. Go to Genova \n"; 
cout << "2. Go to Jebusland \n"; 
cout << "3. Quit \n"; 
cin >> choice;
return
choice; 
}

since you are using vectors, and the vectors already contains data, then
all you have to do is jumble up the data into different place. Therefore
there is a function called std::random_shuffle that does just that.

Here is the link. Unsurprisingly, they use vectors as an
example.

what's interesting he already used random_shuffle at line #108:

random_shuffle(item.begin(), item.end());

Then whats the problem ? Why can't he just use that?

Don't ask me. No idea:)

thanks for the reply back you guys the random shuffle did work i was trying to input srand(time(0)) into the vector then jumble up but i reallized i didnt needed that i just needed to use random shuffle

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.