我正在寻找对数组排序的帮助。我必须这样做,但我收到一些我不理解的错误消息,例如:
- [警告“索引”的名称查找已更改。
- 根据 ISO 标准规则匹配此 'char* 索引 [const char*, int]
- 根据旧规则匹配此索引
- 数组下标的无效类型 'int&[char()(const char*, int)]
- 在全局范围内
我有点怀疑它是什么,但有点不知道如何修复它。 我打开的文件是这样一句话:奥利弗是一只金毛猎犬,它的毛又长又金。
如您所知,我是一个完全的初学者,所以任何提示都将不胜感激提前致谢!
#include <iostream>
#include <fstream>
using namespace std;
void swap_values(int& v1, int& v2);
int index_of_smallest(int list[],int start_index, int number_used);
void initialize(int list[]);
void Sort(int list[],int&num);
void characterCount(char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount(int list[]);
int main()
{
int index,letterCount[26];
char ch;
ifstream inFile;
ofstream outFile;
cout<<"This is the text of the file:"<<endl;
outFile.open("C:/temp/Data_Chapter_7_8.txt");
if(!outFile)
{
cout<<"Cannot open file."<<endl;
}
inFile.open("C:/temp/Data_Chapter_7_8.txt");
if (!inFile)
{
cout << " Cannot open file." <<endl;
}
initialize(letterCount);
inFile.get(ch);
while (inFile.get(ch))
{
int index;
readText(inFile,ch,letterCount);
index++;
}
totalCount(letterCount);
inFile.close();
system("PAUSE");
return 0;
}
void initialize(int list[])
{
for(int x = 0;x<26;x++)
list[x] = 0;
}
void characterCount (char ch, int list[])
{
ch = tolower(ch);
if(static_cast<int>(ch)>=97&&(static_cast<int>(ch)<=122))
list[static_cast<int>(ch)-97]++;
}
void readText(ifstream& intext, char& ch, int list[])
{
if (ch != '.')
{
characterCount (ch,list);
}
}
void totalCount(int letterCount[])
{
for(int x=0;x<26;x++)
if(letterCount[x]>0)
cout<<static_cast<char>(x+97)<<" "<<letterCount[x]<<endl;
}
void Sort(int list[], int number_used)
{
int index_of_next_smallest;
for(int index= 0; index<number_used -1; index++)
index_of_next_smallest = index_of_smallest(list, index,number_used);
swap_values(list[index],list[index_of_next_smallest]);
}
}
int index_of_smallest(int list[], int start_index, int number_used);
{
int min = list[start_index];
index_of_min = start_index;
for (int index= start_index + 1; index < number_used; index++)
if (list[index]>min)
{
min = list[index];
index_of_min = index;
}
return index_of_min;
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
最佳答案
在您的 Sort
函数中,更改
for(int index= 0; index<number_used -1; index++)
到
int index;
for(index = 0; index < number_used-1; index++)
因为你需要在循环结束后访问index
。
关于c++ - 排序数组文件 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13437151/