我正在尝试使用冒号排序将我的数据类型Student
数组student_database
与其成员ID数组从最小到最大排序。我从C ++教科书中复制了一个例子。程序编译,但它对数组没有任何作用。我需要使用指针吗?我很困惑,谢谢你。
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
struct Student
{
int ID;
int examscore[3];
int examtotal;
string lettergrade;
};
void inputinfo(Student[], int&);
void lettergrade(Student[], int);
void countgrades(Student[], int);
void IDsort(Student[], int);
void sorthi(Student[], int);
void sortlow();
void maxexam(Student[], int, int);
void outputall(Student[], int);
void outputstudent(Student[]);
ifstream infile;
ofstream outfile;
int main()
{
string filename;
int numofstudents = 0;
int operation;
int user;
cout << "Enter the filename: ";
cin >> filename;
infile.open(filename);
if (infile.fail()) // checks to see if file is opened correctly
{
cout << "FILE_OPEN_FAILURE > CHECK_TO_SEE_IF_THE_FILE_IS_IN_THE_SAME_FOLDER \n_AS_YOUR_PROGRAM_FILE \nCHECK_YOUR_SPELLING_AS_WELL" << endl;
}
Student student_database[300];
inputinfo(student_database, numofstudents);
outputall(student_database, numofstudents);
cout << endl << "Class size of: " << numofstudents << endl;
do {
cout << "Welcome. Enter a number from the menu to display the requested information." << endl << "---------------------------------------------------------" << endl;
cout << "1. Student with the highest score on exam 1" << endl;
cout << "2. Student with the highest score on exam 2" << endl;
cout << "3. Student with the highest score on exam 3" << endl;
cout << "4. Students ID in ascending numerical order" << endl;
cout << "5. Sort total exam scores from least to greatest" << endl;
cout << "6. Sort total exam scores from greatest to least" << endl;
cout << "7. Total number grade results for the entire class" << endl;
cin >> operation;
cout << endl;
switch (operation)
{
case 1:
maxexam(student_database, numofstudents, operation);
break;
case 2:
maxexam(student_database, numofstudents, operation);
break;
case 3:
maxexam(student_database, numofstudents, operation);
break;
case 4:
IDsort(student_database, numofstudents);
break;
case 5:
sorthi(student_database, numofstudents);
break;
case 6:
//sortlow();
break;
case 7:
countgrades(student_database, numofstudents);
break;
default:
break;
}
cout << "Would you like to request more information?";
cout << endl << "1. Yes? 0. No?" << endl;
cout << "Answer: ";
cin >> user;
cout << endl << endl;
} while (user == 1);
}
void IDsort(Student student_database[], int numofstudents)
{
bool swap = false;
Student temp;
while (!swap)
{
swap = true;
for (int i = 0; i < (numofstudents - 1); i++)
{
if (student_database[i].ID > student_database[i + 1].ID)
{
temp = student_database[i];
student_database[i] = student_database[i + 1];
student_database[i] = temp;
swap = false;
}
}
}
}
答案 0 :(得分:1)
此交换错误:
temp = student_database[i];
student_database[i] = student_database[i + 1];
student_database[i] = temp;
您更改了student_database[i]
,但随后将其重新分配给之前的值。您应该更新student_database[i + 1]
。
student_database[i + 1] = temp;
最好还是使用std::swap()
:
std::swap(student_database[i], student_database[i + 1]);