lundi 2 octobre 2017

Trying to bubble sort array of struct with array of pointers, returns Exception thrown: read access violation. 0xCCCCCCE0

Whenever I try to sort my program freezes and opens xstring, the details of the error say Exception thrown: read access violation. std::_String_alloc > >::_Mysize(...) returned 0xCCCCCCE0. occurred

I read something about uninitialized pointers being part of the problem, if that helps at all. I've tried declaring and assigning the "temp" pointer and nothing works.

Here is my code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace std;

//structure declaration
struct StudentType                                                                              
//structure that holds student information 
{
string firstName;                                                                           
//holds the first name of the student
string lastName;                                                                            
//holds the last name of the student
string sID;                                                                                 
//holds student id number
string studentEmail;                                                                        
//holds student email address
float gpa;                                                                                  
//holds the grade point average of the student
};

//global scope variable declares
const int MAX_ARRAY_SIZE = 15;

//protyping
void printRecords(StudentType *[]);
StudentType grabRecords(StudentType[], StudentType *[]);
StudentType sortRecords(StudentType *[], int sortingField);

int main()
{
//declares
StudentType studentRecords[MAX_ARRAY_SIZE];                                                 
//array of size fifteen that holds fifteen students
StudentType *studentSorter[MAX_ARRAY_SIZE];                                                 
//array of pointers of type StudentType
int menuOptions;                                                                            
//integer used to determine what action the user wishes to execute
int sortingField;                                                                           
//integer used to determine what sorting field the user wishes to sort by


//assigment
menuOptions = 0;                                                                            
//the value of zero for this variable represents an idle state
sortingField = 0;                                                                           
//the value of zero for this variable represnts an idle state



//menu options
cout << "What would you like to do?" << endl;
cout << "\n1 to print unsorted student records." << endl;
cout << "\n2 to sort the student records by a specific field." << endl;
cout << "\n3 to close the program." << endl;

cin >> menuOptions;

switch (menuOptions)
{
case 1:
    cout << "Printing unsorted records..." << endl;
    //grab records
    grabRecords(studentRecords, studentSorter);
    //print records
    printRecords(studentSorter);
    break;
case 2:
    cout << "Please select how you would like to sort your records." << 
endl;
    sortRecords(studentSorter, sortingField);
    break;
case 3:
    cout << "Goodbye." << endl;
    return 1;
    break;
default:
    break;
}

system("pause");
return 0;
}

StudentType grabRecords(StudentType studentRecords[], StudentType 
*studentSorter[])
{
//declares
ifstream inFile;                                                                            
//input file
int i = 0;                                                                                  
//used as a counter


//open input file
inFile.open("studentRecords.txt");

//reading info from file into array of structures
while (!inFile.eof())
{
    for (i = 0; i < MAX_ARRAY_SIZE; ++i)
    {
        inFile >> studentRecords[i].firstName;
        inFile.ignore();
        inFile >> studentRecords[i].lastName;
        inFile.ignore();
        inFile >> studentRecords[i].sID;
        inFile.ignore();
        inFile >> studentRecords[i].studentEmail;
        inFile.ignore();
        inFile >> studentRecords[i].gpa;
        inFile.ignore();
    }
}

//transfer studentRecords to studentSorter
for (int j = 0; j < MAX_ARRAY_SIZE; ++j)
{
    studentSorter[j] = &studentRecords[j];
}

return **studentSorter;

//close files
inFile.close();
}

void printRecords(StudentType *studentSorter[])                                                 
//function that prints both records sorted or unsorted
{
//declares
ofstream outFile;                                                                           
//output file


//opens output file
outFile.open("sortedStudentRecords.txt");

for (int i = 0; i < MAX_ARRAY_SIZE; ++i)
{
    cout << studentSorter[i]->firstName << "\t"
        << studentSorter[i]->lastName << "\t"
        << studentSorter[i]->sID << "\t"
        << showpoint << setprecision(3)                                                     
//used to make formatting look cleaner
        << setfill(' ');

    if (studentSorter[i]->studentEmail.length() < 24)                                       
//used to help with formatting gpa

    {

        cout << studentSorter[i]->studentEmail << "\t"
            << right << setw(12)
            << studentSorter[i]->gpa << endl;

    }


    else
    {
        cout << studentSorter[i]->studentEmail << "\t"
            << studentSorter[i]->gpa << endl;
    }
}

//close output file
outFile.close();

}

StudentType sortRecords(StudentType *studentSorter[], int sortingField)
{
//declares
bool swapNeeded;                                                                            
//variable used to determine whether or not the array needs to continue 
being sorted

//assignment
sortingField = 0;                                                                           
//the value of "0" for this variable represents an idle state
swapNeeded = true;


//sorting options
cout << "What would you like to sort your records by?" << endl
    << "\n1 sort by FIRST NAME in ALPHABETICAL ORDER." << endl
    << "\n2 sort by FIRST NAME in REVERSE ALPHABETICAL ORDER." << endl
    << "\n3 sort by LAST NAME in ALPHABETICAL ORDER." << endl
    << "\n4 sort by LAST NAME in REVERSE ALPHABETICAL ORDER." << endl
    << "\n5 sort by STUDENT ID NUMBER in ASCENDING ORDER." << endl
    << "\n6 sort by STUDENT ID NUMBER in DESCENDING ORDER." << endl
    << "\n7 sort by STUDENT EMAIL in ALPHABETICAL ORDER." << endl
    << "\n8 sort by STUDENT EMAIL in REVERSE ALPHABETICAL ORDER." << endl
    << "\n9 sort by GPA in ASCENDING ORDER." << endl
    << "\n10 sort by GPA in DESCENDING ORDER." << endl;

cin >> sortingField;

switch (sortingField)
{
case 1: 
    {
        while (swapNeeded)
        {
            swapNeeded = false;

            for (int j = 0; j < (MAX_ARRAY_SIZE - 1); ++j)
            {
                if ((*(studentSorter + j))->firstName < (*(studentSorter + j 
+ 1))->firstName)
                {
                    swapNeeded = true;

                    StudentType *temp = *(studentSorter + j);
                    *(studentSorter + j) = *(studentSorter + j + 1);
                    *(studentSorter + j + 1) = temp;
                }
            }
        }
    }

}

return **studentSorter;
}

Aucun commentaire:

Enregistrer un commentaire