0

I am trying to run this but the file is constantly failing to load. What I am trying to do is load a dictionary into an Array with each level of an array accounting for one word.

#include <iostream> #include <string> #include <fstream>> #include <time.h> #include <stdlib.h> using namespace std; int Rand; void SetDictionary(){ srand(time(NULL)); Rand = rand() % 235674; fstream file("Hangman.txt"); if(file.is_open()){ string Array[235675]; for(int X = 0; X < 235673; X++){ file >> Array[X]; } cout << Array[Rand]; }else{ cout << "Unable To Open File\n"; } } int main(){ SetDictionary(); } 
5
  • 1
    What happens if the file does not have exactly 235675 words? Commented Apr 1, 2014 at 23:54
  • I'm pretty sure this is some environment issue; set the right path and run from the right directory relative to it. Commented Apr 1, 2014 at 23:56
  • As you can probably tell I am a beginner at C++ and the file actually has 235674 words (I fixed that problem I think).Thanks. Commented Apr 1, 2014 at 23:59
  • How do I set the path? Sorry. Commented Apr 1, 2014 at 23:59
  • The directory you're running the executable file from (not where the executable file is - that doesn't matter) and the directory where the file read from should be the same. Commented Apr 2, 2014 at 0:01

2 Answers 2

4
vector<string> words; { ifstream file("Hangman.txt"); string word; while (file >> word) { words.push_back(word); } } string randword = words[rand() % words.size()]; 
Sign up to request clarification or add additional context in comments.

4 Comments

If the file is known to have a large number of words, you could speed this up a bit by pre-allocating memory, e.g. words.reserve(235674).
@MattMcNabb Well it looks like he only wants one word out of the whole bunch, so it could be sped up many ways :)
Yep. I was assuming that he would later want to do other things with the loaded dictionary, but judging by the name of the program, maybe not.
I was going to use it for other things after making the hangman game
0

At first, I see you do not reuse Array after cout << Array[Rand] is done. You do not need array at all in this case. Read the file line by line into temp variable and cout this variable if condition X==Rand, then break. At second, the implementation could be improved. Assumed you are trying to cout random word from file. It would be 1000-times faster to generate Rand as 0..file-size, then offset to this Rand. Now you are "inside" desired word and the task is to read back and forward for the work begin and end respectively. This algorithm will show a bit different probability distribution. At third. If you plan to reuse file data, it would be much faster to read whole file into memory, and then do split by words, storing words offsets as arrays of integers. At last. With really huge dictionaries (or if the program run on limited memory) it is possible to store words offsets only, and re-read dictionary contents on-the-fly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.