0

I'm a student and I'm trying to make a hangman program. I'm given a file called words.dat that I'm supposed to use for my hangman program, but I don't know how to store the data and put it into a string array. I'm trying to create a function dedicated to importing the file. I don't know how may words will be in the dat file but my teacher said there would be 100 at most. This was my rough guess on how to do it, but I only get a lot of errors trying to compile it. How do I get the words from the dat file into the string array?

words.dat

COW
SCHOOL
KEY
COMPUTER

my function so far

#include <string>
#include <fsteam>
using namespace std;

bool initializeFile(string filename){
    int importWord = 0;
    string words[100];

    ifstream input;
    input.open(filename, 'r');
    if(input.fails){
        return false;
    }        
    /*
    Tring to make for loop that runs through 
    all the characters in input(dat file) and 
    store them into the words array. if the character isn't a letter it
    skips it and continues to the next row
    */

    for(int i=0;i< input.length();i++){
        if(input[i] =="\n"){
            importWord++;
        }
        if(input[i] != "\n"){
            words[importWord]=input[i];
        }
    }

    return true;
}
CSstudent
  • 669
  • 2
  • 7
  • 27
  • Are you familiar with the `std::vector` type? That's probably a better tool to use than raw C++ arrays. – templatetypedef Oct 01 '17 at 22:30
  • You should probably find some tutorial about files and reading files in C++, because neither [`std::ifstream`](http://en.cppreference.com/w/cpp/io/basic_ifstream) nor any of its bases have a `length` member function, or are used like that to read from it. Perhaps even [get a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Oct 01 '17 at 22:33
  • And you might be interested in learning about the [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) function? – Some programmer dude Oct 01 '17 at 22:34
  • no im not familiar std::vector – CSstudent Oct 01 '17 at 22:36
  • 1
    Possible duplicate of [Reading line from text file and putting the strings into a vector?](https://stackoverflow.com/questions/8365013/reading-line-from-text-file-and-putting-the-strings-into-a-vector) – Henri Menke Oct 01 '17 at 22:49
  • Your array `words` is declared as a *local* variable in the function. The array will vanish when execution leaves the array. You should probably pass an array or put the input code into your `main` without a function. – Thomas Matthews Oct 01 '17 at 22:53

1 Answers1

0

Since you have one word per line, you can use std::getline to read in the words.

const int MAXIMUM_WORDS = 100;
std::string words[MAXIMUM_WORDS];
std::string word_from_file = 0;
int word_count = 0;
while (std::getline(word_file, word_from_file))
{
  if (word_count > MAXIMUM_WORDS)
  {
    break;
  }
  words[word_count] = word_from_file;
  ++word_count;
}

If you have covered the break statement, you can use multiply conditions:

while ((word_count < MAXIMUM_WORDS) && (std::getline(word_file, word_from_file))

The above code is small enough that you can put the code into your main function, which eliminates the hassle of passing an array to an input function.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154