Beginner here so please be patient. I am having a bit of a trouble using a list of words for a simple hangman program that I am creating as an exercise. I am using this code to read a list of words from a text file:
vector<string> getWords()
{
vector<string> text_file;
ifstream ifs( "my_hangman_words.txt" );
string temp;
while(getline(ifs, temp))
{
text_file.push_back( temp );
};
return text_file;
}
This works fine when I compile and run directly but then it does not when I run the executable individually. From what I understand I need to write the vector to a file and #include the file with my program. Could someone give me a pointer on how to achieve that?
#includes are for bringing in code not resources.The lack of an absolute path (i.e.
c:/workingpath/file.txt) is what it likely preventing your code from reading in the file correctly when run directly.When running in the debugger you can set a number of extra parameters including command arguments and working path (links for Visual Studio). These aren’t applied when running the binary directly.
If you could be more specific with what happens when you run directly we can help more.