Reading Special Words from plain text File

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.
Last edited on
Why do you need the ID in the text file?

wordList.txt:
5
Hello
Car
Dog
Cat
Mice


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

std::string getRandomWord()
{
    std::ifstream in("wordList.txt");
    unsigned nWords;
    in >> nWords;

    unsigned word = std::rand() % nWords + 1;

    std::string text;
    for (unsigned i = 0; i < word; ++i)
        in >> text ;

    return text;
}

int main()
{
    std::srand((unsigned) std::time(0));

    for (unsigned i = 0; i < 3; ++i)
        std::cout << getRandomWord() << '\n';
}


Or, better yet, just load all of the words into a vector once and pick one randomly from the vector when you need one.
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((unsigned int)t);

    for ( i=0; i<20; i++ )
    number = rand() % limit + 1;
    return number;
  }


main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main () 
{
	int index;
    cout << "------------------------------Hangman------------------------------\n" << endl;
	
	
	ifstream fileToRead;
	fileToRead.open("wordList.txt");
	if(fileToRead.is_open()) {
		if(fileToRead.good()) {
			cout << "List loaded!\n" << endl;
		}
	} else {
		cout << "List could not be loaded.\nPlease check if List exists!\n" << endl;
	}

	index = randomNumber(3);

       getch();
       fileToRead.close();
       return 0;
}
Last edited on
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.
So how would I do it using vectors?
Never worked before with vectors so I will start reading the reference page in a second.
It would be so much easier using the search functions(vectors).
here is how you can use it:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool randomNumber(int limit)
 {
    int i;
    int number;
    srand(time(0));
    for ( i=0; i<20; i++ )
    number = rand() % limit + 1;
    return number;
 }
int main()
{
    vector<string> vec;
    // push back calls
    
   std::vector<string>::iterator it = std::find_if (myvector.begin(),myvector.end(), randomNumber);
 }


searching functions are of type bool(predicate functions)
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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

std::string getRandomWord()
{
    static std::vector<std::string> words;

    if (words.size() == 0)         // words haven't been loaded yet
    {
        std::ifstream in("wordList.txt");
        std::string word;

        while (in >> word)
            words.push_back(word);
    }

    return words[rand() % words.size()];
}

int main()
{
    std::srand((unsigned) std::time(0));

    for (unsigned i = 0; i < 3; ++i)
        std::cout << getRandomWord() << '\n';
}
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!

Topic archived. No new replies allowed.