C++ 将调用从.txt转换为字符串c++;

C++ 将调用从.txt转换为字符串c++;,c++,C++,我试图从一个文本文件中读取,然后计算每个单词出现的次数,然后将结果导出到另一个文本文件。我只允许在这个任务中使用循环和数组。我只是想在正确的方向上稍微推动一下,主要是在代码的开头。它编译不正确 using namespace std; #include <iostream> #include <iomanip> #include <string> #include <fstream> void put(string word, string w

我试图从一个文本文件中读取,然后计算每个单词出现的次数,然后将结果导出到另一个文本文件。我只允许在这个任务中使用循环和数组。我只是想在正确的方向上稍微推动一下,主要是在代码的开头。它编译不正确

using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


void put(string word, string words[], int counts[], int & size)

{
 int w;
 for (w = 0; w < size && word >= words[w]; w++);
 if (w >= size) {
 words[w] = word;
 counts[w] = 1;
 ++size;
}
else if (word < words[w]) {

for (int i = size - 1; i >= w; i--) {
 words[i + 1] = words[i];
 counts[i + 1] = counts[i];
}
words[w] = word;
counts[w] = 1;
++size;
}
else {
counts[w] = counts[w] + 1;
}
}

int main()
{
 int word;

 ifstream input("input.txt");
 ofstream chout("charcount.txt");
 ofstream wout("wordscounts.txt");


 int inputSize = sizeof(input) / sizeof(string);
 int counts[100];
 const int MAX = 100;
 string words[MAX];
 int wordsSize = 0;


 while (input >> word) {
  put(input[word], words, counts, wordsSize);
 }



 wout << "       Word  Frequency" << endl;

for (word = 0; word < inputSize; ++word) {
 wout << setw(10) << words[word] << setw(4) << counts[word] << endl;
 }




chout.close();
wout.close();

system("pause");
return 0;
}
使用名称空间std;
#包括
#包括
#包括
#包括
无效放置(字符串字、字符串字[]、整数计数[]、整数和大小)
{
int w;
对于(w=0;w=words[w];w++);
如果(w>=尺寸){
单词[w]=单词;
计数[w]=1;
++大小;
}
else if(单词<单词[w]){
对于(int i=size-1;i>=w;i--){
字[i+1]=字[i];
计数[i+1]=计数[i];
}
单词[w]=单词;
计数[w]=1;
++大小;
}
否则{
计数[w]=计数[w]+1;
}
}
int main()
{
int字;
ifstream输入(“input.txt”);
流chout(“charcount.txt”);
ofstream wout(“WordSconts.txt”);
int inputSize=sizeof(输入)/sizeof(字符串);
整数计数[100];
常数int MAX=100;
字符串字[MAX];
int-wordsSize=0;
while(输入>>单词){
put(输入[字]、字、计数、字大小);
}
这条线是什么

put(input[word], words, counts, wordsSize);
您正在尝试索引到
ifstream
。这是不允许的。编译器(g++4.8.4)说:

与“operator[]”不匹配(操作数类型为“std::ifstream{aka std::basic_ifstream}”和“int”)

不幸的是,这是教授的例子。我将发布他所说的。int-word;string-input[]={…}for(word=0;word>word put(input[word],单词,计数,单词大小);@ZackMcCoskey:所以有
字符串输入[]
ifstream输入
?请在问题中发布完整的代码。
no match for ‘operator[]’ (operand types are ‘std::ifstream {aka std::basic_ifstream<char>}’ and ‘int’)