我是编程初学者。我目前的大学作业告诉我从文件中读取文本并且只获取“有效”单词,即 end
而不是end.
. 我陷入了必须将新检测到的单词放入单词数组中的部分。
编译器错误是:数组必须用大括号括起来的初始化程序初始化
int const MAX_WORDS = 100000;
int const MAX_WORDLENGTH = 80;
typedef char Word [MAX_WORDLENGTH];
typedef Word Words [MAX_WORDS];
Words words ;
bool read_file (char filename [80])
{
ifstream file(filename);
if(!file) {
cout << "wrong filename";
return false;
}
char c;
int word_idx = 0;
Word word = words[word_idx++];
int letter_idx = 0;
int connector_count = 0;
while (file.get(c)) {
if ((c>='A' && c<='Z')||(c>='a' && c<='z'))
{
word[letter_idx++] = c;
cout << c << endl;
}
else {
if (c == '-') {
if(connector_count==0) {
word[letter_idx++] = c;
connector_count++;
}
else {
if(connector_count==1) {
word[letter_idx-1] ='\n';
Word word = words[word_idx++];
}
}
}
}
}