Im writing a program which scrambles a word from a .txt file which I already retrieve with the inputFile fuction. My question is how can i get a word from that list, scramble it, and resend it back to main?
using namespace std;
void WordList(string name[], int SIZE);
void sWord(char name[14],string sName);
int main()
{
const int SIZE = 20;
string names[SIZE];
string mixed;
char old;
WordList(names, SIZE);
cout << " The words in my list are: " << endl;
for(int h = 0; h < SIZE; h++)
{
cout << names[h] << endl;
}
sWord(old, mixed);
return 0;
}
//*****************************************
// Reading Data from a File *
// Purpose: Read data from an array. *
// Given: From main. *
// Return: The list of 20 words. *
//*****************************************
void WordList(string name[], int SIZE)
{
ifstream inputFile;
SIZE = 20;
name[SIZE];
if( !inputFile)
{
cout << " There has been an error trying to open up the program!" << endl;
cout << " The program will now terminate!" << endl;
}
cout << " Reading data from the file. \n";
inputFile.open("WordList.txt");
for( int h = 0; h < SIZE; h++)
{
inputFile >> name[h];
}
inputFile.close();
}
//*********************************************************
// Word Scrambler!!! *
// *
// Purpose: Take a word from list and scramble it. *
// Given: An unscrambled word from a give list. *
// Return: A scrambled word in replace of the original. *
//*********************************************************
Using rand() in your scramble function, there are many many ways to decide how to scramble the word. One easy way would be to randomly choose two elements and swap them, and repeat this a fixed number of times. That is an easy way, but it is not truly random. Another way might be generating a random number for each character in the string, then sorting the characters according to the number associated with them. There are tons of other ways. You could probably look up some ideas by googling for randomizing an array or card shuffling (which is a similar problem).
ok I have 20 words in a list, and i want to choose one of those words to scramble, manually though. I have a word called "ray" which is defined as "name[14]". I want to assign different letters in different spots so that it will look like "yar" or "rya". Then i want to send it back.
Psuedocode:
What is number of the word you want to scramble? 14
Word is 'ray'
What do you want to do? switch 1/3
Word is 'yar'
etc...
If you wanted to do something like that, then you would have to build sort of a command parser...unless you HAVE to do it manually, it would be easier to just scramble it randomly.