【问题标题】:Sorting an Array File I/O对数组文件 I/O 进行排序
【发布时间】:2023-04-10 03:34:01
【问题描述】:

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

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

【问题讨论】:

  • 我强烈建议您不要为两个不同的变量使用相同的名称。这将立即解决至少一半的问题。
  • 好的,我已经这样做了,现在我发现 index1 的名称查找错误已更改为新的 ISO 'for' 范围。以及“使用过时的绑定和 index1”

标签: c++ arrays


【解决方案1】:

在您的 Sort 函数中,更改

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

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

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

【讨论】:

  • 谢谢,程序正在编译,但显示一个空白屏幕,有什么调试方法吗?
  • @user1705380 尝试使用调试器单步执行。
猜你喜欢
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多