Basically I need to change the Two StudentType* functions to const but i know its not letting me because of the &ersand sign on the ReadStudentData function, is there anyway around this?

I am not asking for someone to do my hw just point me in right direction

it brings up a whole bunch of errors when I try to throw const in it

this is what i am trying to get (StudentType * SortStudentByName( const StudentType* student, int size);

void ReadStudentData(ifstream& infile, StudentType*& student, int& numOfStudents)

{

    string firstName, lastName;

    int testScore;

    int count = 0;


    infile >> numOfStudents;

    try

    {

        student = new StudentType[numOfStudents];

        while( infile >> firstName >> lastName >> testScore)

        {

            if( testScore >= 0 && testScore <=100)

            {

                student[count].studentName = lastName + ", " + firstName;

                student[count].testScore = testScore;

                count++;

            }


        }


        numOfStudents = count;

    } catch (bad_alloc)

    {

        cout << "Failed to allocate memory" << endl;

        numOfStudents = 0;

        student = NULL;

    }


}


StudentType* 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;

        }



        StudentType temp = student[minIndex];

        student[minIndex] = student[startScan];

        student[startScan] = temp;



    }


    cout << endl;

    cout << "List of Students sorted Alphabetically "<< endl;

    DisplayAllStudents(student, numStudents);

    return student;

}


StudentType* 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;

        }



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

    return student;

}

Recommended Answers

All 2 Replies

Your functions modify the elements of the array (i.e., swaps them during the sorting). There is no way that you can declare that as const, because that implies a promise never to modify the contents, but your functions require that you do so.

Also on number 1. void ReadStudentData(ifstream& infile, StudentType*& student, int& numOfStudents)
whats the point of int &numOfstudents as a reference if you just write to it on line 12 infile >> numOfStudents; like this. If you ntend to write to it like this, then there is not need to even use it in the parameter at all.

I personally think that you need to design your functions very well...
Also like what mike has already stated, what you want to do and what your function is asking to do contradicts to each other.

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.