hello, I am having problem with selection sort, I am trying to sort the students name alphabetically.I compile it and it shows me whole bunch of errors in VS.

I dont think it has do to do with my display all students function, I think it has more to do with SortByName and SortByScoreFunctions

any help is appreciated thank you!

here is the struct for my program.

struct StudentType
{
    string studentName;
    int testScore;
    char grade;
};




    void SortStudentsByName(StudentType student[], int numStudents)
    {
        int startScan, minIndex, FirstInAlphabet;

        for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++)
        {

            minIndex = startScan;
            FirstInAlphabet = student[0];

            for( int index = startScan+1; index < NUM_STUDENTS; index++)
            {
                if ( student[index] > FirstInAlphabet)
                {
                    FirstInAlphabet = student[index];
                    minIndex = index;
                }
            }
        }
    }





        void SortStudentsByScore(StudentType student[], int numStudents)
{
    int startScan,
        minIndex,
        lowest;



    for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++)
    {
        minIndex = startScan;
        lowest = student[0].testScore;

        for ( int index = startScan+1; index < NUM_STUDENTS; index++)
        {
            if( student[index].testScore < lowest)
            {
                lowest = student[index].testScore;
                minIndex = index;
            }
        }

        student[minIndex].testScore = student[startScan].testScore;
        student[startScan].testScore = lowest;

        cout <<"List of Students sorted by Score from Highest to Lowest" << endl;
        DisplayAllStudents(student, numStudents);
    }

}




void DisplayAllStudents(const StudentType student[], int numStudents)
{
    cout << endl;

    FormatNameScoreGrade(cout);

    for(int i = 0; i < numStudents; i++)
    {

            cout << setw(20) << student[i].studentName  << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
    }

    cout << endl;

    EndOfList(cout);
}

Recommended Answers

All 3 Replies

Saying you get a bunch of errors doesn't help anyone give an answer. You have to itemize the error messages you're getting, especially the line it's pointing to.

One problem you have is you're using NUM_STUDENTS but it's not defined any where in your code.

Another problem, you're trying to cast a struct object as an integer, FirstInAlphabet = student[0];

sorry, my mistake there are no error messages.
these are my output results for the Sort by Name

Fibonacci, Leonardo        63         D
     Huffman, David        79         C
       Augusta, Ada        91         A
Goldbach, Christian        81         B
         Venn, John       100         A
     Church, Alonzo        72         C
     Fermat, Pierre        84         B
    Kruskal, Joseph        66         D
      Cantor, Georg        67         D
       Turing, Alan        85         B
     Chebysheva, PL       100         A
 DeMorgan, Augustus        79         C
  Karnaugh, Maurice        72         C
   Babbage, Charles        98         A
      Hooper, Grace        95         A

its obviously not working here

and this is the output for my sort by highest grade

Student Name          Test Score     Grade
------------------------------------------
                   -858993460         D
     Huffman, David-858993460         C
       Augusta, Ada-858993460         A
Goldbach, Christian-858993460         B
         Venn, John-858993460         A
     Church, Alonzo-858993460         C
     Fermat, Pierre-858993460         B
    Kruskal, Joseph-858993460         D
      Cantor, Georg-858993460         D
       Turing, Alan-858993460         B
     Chebysheva, PL-858993460         A
 DeMorgan, Augustus-858993460         C
  Karnaugh, Maurice-858993460         C
   Babbage, Charles-858993460         A
      Hooper, Grace-858993460         A

worked on it for a while and came up with this, works perfect!

void SortStudentsByName(StudentType student[], int numStudents)
    {

            int startScan,
            minIndex;

        for (startScan = 0; startScan < (numStudents-1); startScan++)
        {
            minIndex = startScan;
            for ( int index = startScan; index < numStudents; index++)
            {
                if( student[index].studentName < student[minIndex].studentName)
                    minIndex = index;
            }
            if(minIndex!=startScan)
            {
                StudentType temp = student[minIndex];
                student[minIndex] = student[startScan];
                student[startScan] = temp;
            }
        }

    cout << endl;
    cout << "List of Students sorted Alphabetically  "<< endl;
    DisplayAllStudents(student, numStudents);

    }



    void SortStudentsByScore(StudentType student[], int numStudents)
    {

        int startScan,
            minIndex;

        for (startScan = 0; startScan < (numStudents-1); startScan++)
        {
            minIndex = startScan;
            for ( int index = startScan; index < numStudents; index++)
            {
                if( student[index].testScore>student[minIndex].testScore)
                    minIndex = index;
            }
            if(minIndex!=startScan)
            {
                StudentType temp = student[minIndex];
                student[minIndex] = student[startScan];
                student[startScan] = temp;
            }
        }

        cout <<"List of Students sorted by Score from Highest to Lowest" << endl;
        DisplayAllStudents(student, numStudents);
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.