【发布时间】:2015-09-19 16:53:19
【问题描述】:
目标是从名为“words.txt”的文件中读取一组字符串,并将每个单词保存到数组字符串中。但是,我无法将单词保存并打印到控制台。我认为问题出在我的 GetStrings 函数中,但我不知道为什么。调用 PrintStrings 函数时,控制台不会打印任何内容。这让我觉得要么没有任何东西保存到数组中,要么打印功能不正确。
int main ()
{
int count = 0;
string strings [MAXSTRINGS];
GetStrings(strings);
// cout << strings[1];
PrintStrings(strings, count);
return 0;
}
int GetStrings (string S [])
{
ifstream input ("words.txt");
int count = 0;
while (input >> S[count])
{
count++;
}
input.close ();
return 0;
}
void PrintStrings (string S [], int C)
{
int w = 0;
while (w < C)
{
cout << S[w] << endl;
w++;
}
}
【问题讨论】:
-
你的数组不是
MAXSTRINGS!这就是数组的长度。您的数组名为strings。 -
投票结束此问题为:寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及必要的最短代码在问题本身中重现它。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。
-
哎呀,我错了!
-
这还不错。不好的部分是你没有描述哪里出了问题,你试图解决什么,以及你认为哪里出了问题!如果您描述了这一点,人们将能够帮助您。
-
好的,我会尝试更详细地了解具体的问题。