当前创建的一段代码能够从文本文件输出,我正在读取文本并将每条不同的信息放入数组中。使用相应的数组对数组进行排序
我已经使用了4个不同的数组,因为我想存储4种不同类型的信息。这个代码的工作方式和预期的一样,但我不确定如何按照字母顺序对数据进行排序,并且所有相应的数组都保持一致并在正确的时间输出。
void displayfile(){
string filename1;
string rartist[NUM];
string rtitle[NUM];
string ryear[NUM];
string rcategory[NUM];
ifstream openFile;
int counter = 0;
int continu
bool error = false;
cout << "test";
do{
//Loop to retrieve the file name from user, then open file
do{
cout << "Please enter the name of the menu you would like to open: ";
cin >> filename1;
filename1 += ".txt";
openFile.open(filename1.c_str());
if(openFile.fail()){
cerr << "Check spelling of file name.\n";
error = true;
}
//Storing text from file into arrays
}while(error == true);
while(getline(openFile, rartist[counter], ':') && getline(openFile, rtitle[counter], ':') &&
getline(openFile, ryear[counter], ':') && getline(openFile, rcategory[counter])){
counter++;
}
//outputting the information stored in the array
cout << "ARTIST " << " DVDTITLE " << " YEAR " << " CATEGORY \n";
for(int i = 0; i < counter; i++){
cout << rartist[i] << " " << rtitle[i] << " "
<< ryear[i] << " " << rcategory[i] << "\n";
}
cout << "\n\nIf you would like to read another file, Press 1: ";
cin >> continu;
}while(continu == 1)
}
这是我用来显示当前文本的功能。
最简单的方法是让你有含有相关项,而不是针对每个项目类型的单独阵列结构的一个单个阵列来重组数据。 –
这可能是我需要的,因为我保留此功能并创建一个新的功能来进行排序,作为另一种选择。谢谢你现在看看这个。 – Thecube