2014-12-13 59 views
0

我正在嘗試使用冒泡排序將我的數據類型Student數組student_database與其成員ID數組從最小到最大排序。我從C++教科書中複製出一個例子。該程序編譯,但它對陣列沒有任何作用。我需要使用指針嗎?我很困惑,謝謝。使用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; 
      } 
     } 
    } 
} 
+1

你有過在調試器的代碼臺階? – OldProgrammer 2014-12-13 03:29:19

回答

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]);