0

I am trying to sort my data type Student array student_database with its member ID array from least to greatest using a bubble sort. I copied out an example from a C++ textbook. The program compiles, but it does nothing to the array. Do I need to use pointers? I'm confused, thank you.

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
  • 1
    Have you stepped through the code in the debugger? Commented Dec 13, 2014 at 3:29

1 Answer 1

1

This swap is wrong:

temp = student_database[i];
student_database[i] = student_database[i + 1];
student_database[i] = temp;

You change student_database[i] but then reassign it to its previous value. You're supposed to update student_database[i + 1].

student_database[i + 1] = temp;

Better to use std::swap() anyway:

std::swap(student_database[i], student_database[i + 1]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.