Help With Sorting a Structure Array

OK my assignment is to make a data file with last names and quiz grades. Then read data into structure(structure should be able to hold atleast 100 records) and sort last name in alphabetical order. Then go into sentinel loop that prompts user for students' last name, if the name entered matches that name in my data structure then print the student's name and the quiz average. If the name does not match a name in my data structure then print name not found and repeat prompt. Also terminate loop when the user enters STOP for the student name. Implement binary search algorithm to locate the student's last name. So far Im attempting at sorting the names in alphabetical order and Im getting errors. Can someone please take a look.

This is my data file:

Jones 89 69 96 90 90
Willis 78 56 89 93 99
Star 78 89 90 85 100
Porter 100 98 88 92 95
Huntington 65 56 45 78 66

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

struct student
{
    char Student_lastname[15];
    int quiz1;
    int quiz2;
    int quiz3;
    int quiz4;
    int quiz5;
};
void sortArray(char array[], int size);
int main()
{
    student grades[100];
    const int size = 100;
    double array[size];
    int i = 0;
    //open file
    ifstream infile;
    infile.open("StudentRec.txt", ios::in);
    if (!infile.is_open())
    {
        exit(EXIT_FAILURE);
    }
    //read data file into structure
    for (i = 0; i<5;i++)
    {
        infile >> grades[i].Student_lastname;
        infile >> grades[i].quiz1;
        infile >> grades[i].quiz2;
        infile >> grades[i].quiz3;
        infile >> grades[i].quiz4;
        infile >> grades[i].quiz5;
        cout << grades[i].Student_lastname << " " << grades[i].quiz1 << " " <<
        grades[i].quiz2 << " " << grades[i].quiz3 << " " << grades[i].quiz4 << " " <<
        grades[i].quiz5 << endl;
    }
    //close file
    infile.close();
    // sort names alphabetically
    //sortArray(5, 5);
   


    return 0;
}//main
void alphabetical(int entries =5 , student grades[],int size)
{
    for (int j=1; j<=entries;j++)
    {
        for(int i=1;i<=entries-j;i++)
        {
            if (strcmp(grade[i].Student_lastname >= grades[i+1].Student_lastname==1)
                {
                    temp[i] = person[i+1];
                    grades[i+1]=grades[i];
                    grades[i]=temp[i];
                    flag=1
                }
        }//for i
    }//for j

}
Im getting errors

What errors?

To learn to interpret the compiler messages is useful. If you post the errors with the code, then we can help you to "see".
Im sorry im getting errors like this at line 54 "error :default argument missing for parameter 2 of 'void alphabetical(int, student*, int)'
It is good to start with the first error, because later errors can be "just side-effects" rather than novel errors.

Line 54:
void alphabetical( int entries =5 , student grades[], int size )
line 54 "error :default argument missing for parameter 2 of 'void alphabetical(int, student*, int)'

Lets call that function:
1
2
3
4
student arr[7];
alphabetical( 5, arr, 3 ); // this would be syntactically ok
// but why write all parameters, if the defaults are ok?
alphabetical( arr, 3 ); // error, there is no alphabetical( student*, int ) 

If a function argument has a default, then all arguments following it must have a default too.

You can change the function signature to:
void alphabetical( student grades[], int size, int entries=5 )
Remember to fix the calls too (for example, my line 2 will break).
I tried this but I don't think im understanding how to call this function with a structure array being one of the arguments. Im also trying to do a binary search function and im having the same misunderstanding. I commented the binary search trying to deal with one problem at a time but you are welcome to take a look.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

struct student
{
    char Student_lastname[15];
    int quiz1;
    int quiz2;
    int quiz3;
    int quiz4;
    int quiz5;
};
void sortArray(student grades[], int size, int entries);
//int binarySearch(char grades[].Student_lastname, int, int)
int main()
{
    student grades[100];
    const int size = 100;
    double array[size];
    int i = 0;
    //open file
    ifstream infile;
    infile.open("StudentRec.txt", ios::in);
    if (!infile.is_open())
    {
        exit(EXIT_FAILURE);
    }
    //read data file into structure
    for (i = 0; i<5;i++)
    {
        infile >> grades[i].Student_lastname;
        infile >> grades[i].quiz1;
        infile >> grades[i].quiz2;
        infile >> grades[i].quiz3;
        infile >> grades[i].quiz4;
        infile >> grades[i].quiz5;
        cout << grades[i].Student_lastname << " " << grades[i].quiz1 << " " <<
        grades[i].quiz2 << " " << grades[i].quiz3 << " " << grades[i].quiz4 << " " <<
        grades[i].quiz5 << endl;
    }
    //sort array
    alphabetical(5,size, 5);
    
    //binary search
    //int results;
    //results = binarySearch(Student_lastname, size, );
    
    //prompt user with sentinel loop
    
    //close file
    infile.close();


    return 0;
}//main
void alphabetical(student grades[], int size, int entries =5)
{
    for (int j=1; j<=entries;j++)
    {
        for(int i=1;i<=entries-j;i++)
        {
            if (strcmp(grade[i].Student_lastname >= grades[i+1].Student_lastname==1)
                {
                    temp[i] = person[i+1];
                    grades[i+1]=grades[i];
                    grades[i]=temp[i];
                    flag=1
                }
        }//for i
    }//for j

}
/*int binarySearch(char grades[].Student_lastname, int size, int value)
{
    int first = 0,
        last = size - 1,
        middle,
        position = 1;
    bool found = false;

    while(!found && first <= last)
    {
        middle = (first + last)/2;
        if (array[middle] == value)
        {
            found = true;
            position = middle;
        }
        else if (array[middle]>value)
            last = middle -1;
        else
            first = middle + 1;
    }
    return position;
}*/
Last edited on
You've defined the first argument to alphabetical() to be an array of student objects. Obviously, 5 can't possibly be an appropriate value to pass in. You need to pass in the things your function is expecting you to pass.
Ok so if I want the function to pass the array of student last names then should it be alphabetical(char Student_lastname, size, 5) . Because Student_lastname is a char type in a structure does it work the same as if it were int type?
Last edited on
There is no array of student last names , as your code currently stands. You have an array of student objects - and that array is the thing that your function is currently defined to take as its first argument. Each object contains a single char array, holding a single last name.

You've already written your alphabetical() method to take the array of objects, and to look at the Student_lastname member of each object in that array, so why do you want to do anything else but pass that array of objects in?
Last edited on
Topic archived. No new replies allowed.