I'm trying to create a simple text-based word jumble game. Basically, I'm trying to create a bank of jumbled words. Ppl can try to solve the jumble. If it's right, it will cout something about having it right. If it's wrong, it will cout something about sorry, but it should have been "answer".

How would I go about implementing the srand function to make this "word bank" of jumbled words be drawn from at random?

Have a text file with 2 columns. Load the columns into a map<string,string> . Pick a random word from the map by using a function like srand() (keeping in mind that the number must be below the .size() of the map. if the answer they provide equals the value in the map for that key, they get it right.

Have a text file with 2 columns. Load the columns into a map<string,string> . Pick a random word from the map by using a function like srand() (keeping in mind that the number must be below the .size() of the map. if the answer they provide equals the value in the map for that key, they get it right.

Another way is to have a word list file like above, except only with the correctly spelled words. Then you read a word, copy it, do a random shuffle, and use those two strings. That way the jumble will be more likely to change often without the tediousness of editing the file.

Better yet, you can also read the file into a vector and do a random shuffle on it as well as the words so that people who play the game often will have a harder time finding patterns and remembering the word order to win more easily. :)

There is more than one way to do most tasks. You can probably accomplish this task by using index values and an an array of strings. Develop a 2d array of strings to represent each word available. The first column could represent the scrambled word and the second the correct word. Call srand once in the project. call rand % numberOfStringInTheArray to get a ranom int to represent the row of the table to use. Use array[index][0] to get the scrambled word and array[index][1] to get the correct version.

call rand % numberOfStringInTheArray to get a ranom int to represent the row of the table to use.

See Narue's site. :)

Thank you Lerner. I was thinking that using an array would be the easiest way. At least it seems to be for a novice such as myself. :) I'll give this route a try.

Narue's site? Where would I find that at?

Narue's site? Where would I find that at?

Here ya go: The Eternally Confuzzled World of Narue :mrgreen:

Here is what I have so far. It keeps giving me a message that I need to declare the items in the array. I didn't think this had to be done individually since they were part of an array. Then again, I've never really used arrays much before...

//Unjumb

#include <iostream>
using namespace std;

char arrayjum[2][2] = {{BRIDEGROOM, CEPHAS, CLAY },
                      {RIGRMOOBED, SEACHP, YLCA }};

int main()
{    
     
     cout << "Unjumb.  Unjumble Biblical names, places, and other words of importance. \n" << endl;
     cout << "All of the words in Unjumb are from the King James version of the Bible. \n\n" << endl;
     cout << "If you can't figure the word out, the real answer will be given and you'll be given the next word. \n" << endl;     
     cout << "The jumbled words all end up being one word, never any spaces. \n\n" << endl;
     

return 0;
    
}

Better:

//Unjumb
#include <iostream>
using namespace std;
char *arraynonjum [3] = {"BRIDEGROOM", "CEPHAS", "CLAY" };
char *arrayjum[3] = {"RIGRMOOBED", "SEACHP", "YLCA" };
int main()
{
	 cout << "Unjumb. Unjumble Biblical names, places, and other words of importance. \n" << endl;
	cout << "All of the words in Unjumb are from the King James version of the Bible. \n\n" << endl;
	cout << "If you can't figure the word out, the real answer will be given and you'll be given the next word. \
n" << endl;
	cout << "The jumbled words all end up being one word, never any spaces. \n\n" << endl;
for ( int i=0;i< 3;i++)
{
		   cout<<arrayjum[i]<<"="<<arraynonjum[i]<<endl;
}
 return 0;
}

Even better:

//Unjumb
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
	map<string,string> myMap;
	 myMap["RIGRMOOBED"]="BRIDEGROOM";
	 myMap["SEACHP"]="CEPHAS";
	 myMap["YLCA"]="CLAY";
 
	cout << "Unjumb. Unjumble Biblical names, places, and other words of importance. \n" << endl;
	cout << "All of the words in Unjumb are from the King James version of the Bible. \n\n" << endl;
	cout << "If you can't figure the word out, the real answer will be given and you'll be given the next word. \
n" << endl;
	cout << "The jumbled words all end up being one word, never any spaces. \n\n" << endl;
	cout<<"ALL:"<<endl;
for (map<string,string>::iterator i= myMap.begin();i != myMap.end();i++)
{
		 cout<<i->first<<"="<<i->second<<endl;
}
	  cout<<"SPECIFIC:"<<endl;
	 cout<<"RIGRMOOBED is:"<<myMap["RIGRMOOBED"]<<endl;
	 cout<<"YLCA is:"<<myMap["YLCA"]<<endl;
	  cout<<"SEACHP is:"<<myMap["SEACHP"]<<endl;
	 return 0;
}
 
./Temp
Unjumb. Unjumble Biblical names, places, and other words of importance.
All of the words in Unjumb are from the King James version of the Bible.
 
If you can't figure the word out, the real answer will be given and you'll be given the next word.
The jumbled words all end up being one word, never any spaces.
 
ALL:
RIGRMOOBED=BRIDEGROOM
SEACHP=CEPHAS
YLCA=CLAY
SPECIFIC:
RIGRMOOBED is:BRIDEGROOM
YLCA is:CLAY
SEACHP is:CEPHAS

Hmm.

I'm not familair at all w/ mapping, so I do prefer the first example you provided Winbatch. But despite helping a bit, it has brought a few new questions. How do I implement srand to pick jumbled words at random? Also, how do I go about providing a "Sorry, but it should have been" type of output and throwing another jumble out there? I'm supposing that I need to implement an if statement...

Why don't you start with picking the first one and asking the user, then validating his input against the first one. Once you have that working, then change it to continue to ask until he gets it right (ie while loop, etc.). Then start dealing with the random stuff later.

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.