【发布时间】:2020-05-02 18:33:55
【问题描述】:
您好,如果没有正确询问,请原谅我,但我的代码有问题。这是学校的作业,但我正在寻求帮助以理解问题而不是答案,因此我们将不胜感激。
我将附上下面的代码,运行它会给我一个我不明白的错误。我昨天和老师谈过了,他指出了我解决的问题,但它没有再次出现。
虽然我的代码中可能有更多错误,但我想先了解错误的访问错误。任何帮助将不胜感激。
请告诉我将来如何更好地发布问题。
错误线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0):
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector<string> getVector(const string&);
string getName(const string&);
void selectionSort(vector<string>&);
bool search(const string&, const vector<string>&);
void displayResult(const string&, const string&, bool);
void writeToFile(const string&, const vector<string>&);
void reverseVector(vector<string>&);
int main()
{
string boyName, girlName;
bool boyNameFound, girlNameFound;
vector<string> boyNames(getVector("BoyNames.txt"));
vector<string> girlNames(getVector("GirlNames.txt"));
boyName = getName("boy's");
girlName = getName("girl's");
selectionSort(boyNames);
selectionSort(girlNames);
boyNameFound = search(boyName, boyNames);
girlNameFound = search(girlName, girlNames);
displayResult("boy's", boyName, boyNameFound);
displayResult("girl's", girlName, girlNameFound);
writeToFile("Boynames_asc.txt", boyNames);
writeToFile("Girlnames_asc.txt", girlNames);
reverseVector(boyNames);
reverseVector(girlNames);
writeToFile("Boynames_desc.txt", boyNames);
writeToFile("Girlnames_desc.txt", girlNames);
cout<<endl;
system("PAUSE");
return 0;
}
void selectionSort(vector<string> &arr)
{
int startScan, minIndex;
string minValue;
for (startScan = 0; startScan < (arr.size() - 1); startScan++)
{
minIndex = startScan;
minValue = arr[startScan];
for(int index = startScan + 1; index < arr.size(); index++)
{
if (arr[index] < minValue)
{
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}// above is code the teacher provided
// below is where I have written code for the assignment
vector<string> getVector(const string& fileName)
{
string name;
ifstream file;
vector<string> namesFromFile;
file.open(fileName);
while(getline(file, name))
{
namesFromFile.push_back(name);
}
return namesFromFile;
}
string getName(const string& gender)
{
string nameChoice;
cout << "Enter a " << gender << " name, or N if you do not wish to enter a " << gender << " name: " << endl;
getline(cin, nameChoice);
return nameChoice;
}
bool search(const string& nameChoice, const vector<string>& names)
{
for(int i = 0; i < names.size(); i++)
{
if(nameChoice == names.at(i)) // names[i]
{
return true;
}
}
return false;
}
void displayResult(const string& gender, const string& names, bool nameFound)// change this function so either displays name found not found or chose //not to enter
{
if(nameFound)
{
cout << names << "is one of the most popular " << gender << "names." << endl;
}
else if(!nameFound)
cout << names << " is NOT one of the most popular " << gender << " names." << endl;
else if(names == "N")
{
cout << "You chose not to enter a " << gender << " name." << endl;
}
}
void writeToFile(const string& fileName, const vector<string>& names)
{
ofstream outputFile;
outputFile.open(fileName);
for(int i = 0; i < names.size(); i++)
{
outputFile << names[i] << endl;
}
}
void reverseVector(vector<string>& names)
{
string temp = 0;
for(int i = 0; i < names.size(); i++)
{
temp = names[0];
names[0] = names[names.size() - i];
names[names.size() - i] = temp;
}
}
【问题讨论】:
-
在您的代码中某处您正在取消引用一个空指针。你试过调试吗?
-
在 selectionSort 中,您使用无效索引访问 arr,第一个 0 为空。使用迭代器怎么样?向量的大小是 unsigned 所以 0u-1u 不是 -1 所以
startScan < (arr.size() - 1)在 arr 为空 时为真 -
in reverseVector 在第一轮 i 是 0 所以你访问
names[names.size()];withis out of the array,即使你从索引开始1 如果数组的大小至少为 4,则不要反转数组 -
@AlanBirtles 我没有尝试调试,我们还没有完成调试,所以我必须先自我教育。我知道我可以逐步完成该程序,但我只是不知道该怎么做,除非我将所有内容都注释掉并一次将其带入一个函数中。
-
@bruno 老师写了选择排序函数,所以我没有更改它,我只是保持原样。但是,对于您的最后评论,它可能与我的反向向量函数有关,因为这是我创建的函数。因此,如果对此功能有更多详细说明,我将采用它们并开始修复它。我很难理解如何反转向量,这就是我想出的。我期待有关此功能的任何其他建议,并将在此之前继续工作。伙计们,我也非常感谢快速的 cmets。