我有整个程序的这个片段。在这个函数中,特别是在这段程序中,用户输入一个 MLS# 来从数组中删除一个家。第一个“for”语句搜索 MLS#,然后搜索 null 的所有数据。我在下一个问题 “为”声明。在索引为空后将所有数据移动到左侧。 struct数组中存放的数据如下:
struct mlsListing { int mlsNum; // Struct Array holds one line of the struct
double price; // mlsListing
int type;
string zip;
string company;
string realty;
};
const int MAX_LISTINGS = 750;
mlsListing houseData[MAX_LISTINGS];
const int NOT_FOUND = -1;
int targetMLS; // Variable for target MLS
int mlsDelete; // Variable for target MLS found
int mlsCounter;// Counter for finding target MLS
int count; // Array Counter
// Function
void {
cout << "Enter the MLS# you wish to delete: ";
cin >> targetMLS; // User input MLS#
for (mlsCounter = 0; ((mlsCounter < count) && (mlsDelete == NOT_FOUND));
mlsCounter++) {
if (houseData[mlsCounter].mlsNum == targetMLS) {
mlsDelete = houseData[mlsCounter].mlsNum;
houseData[mlsCounter].mlsNum = 0;
houseData[mlsCounter].price = 0;
houseData[mlsCounter].type = 0;
houseData[mlsCounter].zip.clear();
houseData[mlsCounter].company.clear();
houseData[mlsCounter].realty.clear();
}
}
// Shifting indices to the left after deletion?
for (move = mlsCounter;move < count; move++){
houseData[move].mlsNum = houseData[move+1].mlsNum;
houseData[move].price = houseData[move+1].price;
houseData[move].type = houseData[move+1].type;
houseData[move].zip = houseData[move+1].zip;
houseData[move].company = houseData[move+1].company;
houseData[move].realty = houseData[move+1].realty;
}
count--;
}
最佳答案
第二个 for 循环越界。它必须是:
for (move = mlsCounter;move < count - 1; move++)
关于c++ - 在结构未排序数组中将索引向左移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778318/