0

我正在寻找对数组进行排序的帮助。我必须这样做,但我收到一些我不理解的错误消息,例如:

  1. [警告“索引”的名称查找已更改。
  2. 匹配此 'char* index[const char*, int] undr ISO 标准规则
  3. 在旧规则下匹配这个索引
  4. 数组下标的无效类型 'int&[char()(const char*, int)]
  5. 在全球范围内

我对它是什么有些怀疑,但对如何解决它有点迷茫。我打开的文件是这样一句话:奥利弗是一只金毛猎犬,它的毛又长又金。

正如你所知道的,我是一个完整的初学者,所以任何提示将不胜感激提前谢谢!

#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;
 }
4

1 回答 1

1

在你的Sort函数里面,改变

for(int index= 0; index<number_used -1; index++)

int index;
for(index = 0; index < number_used-1; index++)

因为你需要index在循环结束后访问。

于 2012-11-18T03:45:36.937 回答