Scrambling words

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?

P.S: I kno the last function is incomplete




#include <iostream>
#include <fstream>
#include <string>


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. *
//*********************************************************

void sWord(char name[14],string sName)
{
char temp;

temp = name[0]

}




Last edited on
If you want to randomly scramble the word you will have to use srand() and rand() in your implementation.

srand() seeds the random number generator - you do this once at the start of your program. Generally it is best to seed with the current time by giving it time(NULL). Check out: http://www.cplusplus.com/reference/clibrary/cstdlib/srand.html

rand() generates a random number. Check out: http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

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.
So, you are talking about something like this?

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.
Last edited on
Topic archived. No new replies allowed.