【发布时间】:2015-01-25 00:54:38
【问题描述】:
所以我正在开发一个hangman 程序,并且我得到了包含我需要的单词的输入文件。现在的问题是如何将我从文件中输入的单词放入数组中? 任何帮助将不胜感激。谢谢!
enter code here
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_NUMS = 200; // Constant for the maximum number of words.
const int MAX_GUESSES = 8;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";
//function prototypes
char inputLetter();
int findChar(char letter, string word);
string getGuessedWord(string secretWord, string lettersGuessed);
//main function
int main()
{
// holds one word from input file
string secretWord; // holds secret word to be guessed
string words[MAX_NUMS]; // holds list of words from input file
int randomValue; // holds index of secret word
int count = 0; // holds number of words in the file
// Declare an ifstream object named myFile and open an input file
string line;
ifstream myfile ("p4words.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
// Input words from a file into words array
cout << count << " words loaded." << endl;
srand(static_cast<unsigned int>(time(0)));
【问题讨论】:
-
将它们放在数组中,而不是或者除了将单词输出到控制台之外?
std::vector更好,因此您不必担心数组的“最大”大小。
标签: c++ arrays file sorting input