We have to code a Hangman game for school but I am stuck on a little problem.
I have created a plain text file called wordList.txt in which I have a ID and a Word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <List Version 0.1 |
---------------------------
Word ID
Hello 1
Car 2
Dog 3
Cat 4
Mice 5
Now I have created a Function in which I can set a range for a random number.
Min will always be 1 and Max will be my last ID + 1.
So what I planned to do is search the text file for the Random Number and extract the word that is in the same line.
Now I saw the get() and getline() functions but was not completely sure if they do what I want because they search for a Char, so I would have to typecast the random number to a string in before and I hope to find a solution that does work without a typecast. They sound as if they extract everything BEFORE the specific Character.
getline (String where I save the Word, Big Number , ID );
That's how I imagined it, but if I enter getline(word, 255, generateID); it throws of errors...
TL;DR
I would appreciate if some of you would point me to a function which easily searches a Text File for a specific character and then copy the whole line IN the text file except the specified character.
The ID was in there because I thought of using the get() or getline() functions which need a delimiting character to stop after the word.
About the Vector, the Problem is, the Application has to be able to handle a amount of N-Words. Those 5 are just for testing in there.
I think it would slow down the application if I load in a huge number of words, or not?
Ouh and since it will be Hangman, the Word doesn't even has to be shown to the user.
I plan on using .length and place the same amount of "_ " instead.
my random number function looks like this until now
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int randomNumber(int limit)
{
int i;
int number;
time_t t;
time(&t);
srand((unsignedint)t);
for ( i=0; i<20; i++ )
number = rand() % limit + 1;
return number;
}
About the Vector, the Problem is, the Application has to be able to handle a amount of N-Words. Those 5 are just for testing in there.
I think it would slow down the application if I load in a huge number of words, or not?
To search through a file, you need to read the file. One of the slowest operations you'll perform is a file read. So, doing that once and storing the words instead of doing it every time you need a random word will actually speed up the application, not slow it down.
ok so I get it that I need to do push_back calls for the amount of words I want to load into the vector, but I still have trouble understanding how I read the words ouf of the text file word by word and not all at once and without the need to type in the amount of words I want to have.
Thats some prototype Code I came up with, but I would still need a function or a way to store the words row by row in a string variable.
Or do I just parse the Text File as a argument for the push_back function?
Anyway heres the code:
1 2 3 4 5 6 7
vector<string> wordListContainer;
string vectorWordContainer ="";
for (int rowNumber; rowNumber != lastRow+1; rowNumber++)
{
vectorWordContainer = *function to extract word out of row = rowNumber*
wordListContainer.push_back (vectorWordContainer);
}
Big thanks!
To not copy everything, I just took your push_back method.
I do have my own randomNumber function an, even though it is not as good as yours ( mine needs about a second to change ) I still want to use it though.
So again, big big thanks to you guys and I'll mark this as solved then!