Please help fix arrays and structures

Hello everyone! This is a great forum, and it's helped me often. However, this time I have a problem that just lurking has been unable to fix. I have this program that's supposed to use functions to create a student gradebook. As you can see, I made a serious attempt to do it and I don't want someone to do everything for me. However, if someone could help me get rid of a few errors, or clarify a few main points I seem to be missing, I would be eternally grateful. Thank you for reading.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>

using namespace std;

struct StudentRecord {
    char ID[10];
    string Name;
    int exam [4];
    double average;
};


void getStudent(StudentRecord &Stu) {
    string ID;
    cout << "Enter the student's ID number: ";
    cin >> stu.ID;
    cin.ignore();
    while (strlen(stu.ID) != 10) {
        cout << "The Student ID must have 9 characters. " << endl;
        cout << "Enter the student's ID: ";
        cin >> stu.ID;
    }

    string Name;
    cout << "Enter the student's name. " << endl;
    getline (cin, stu.Name);
    while (strlen(stu.Name) < 1 || strlen(stu.Name) > 30) {
        cout << "Error: The student's name must be between 1 and 30 characters. " << endl;
        cout << "Enter the student's name: ";
        cin >> stu.Name;
    }

    int input;
    examarray = new int [4];
    cout << "Please enter four exam scores: " << endl;
    for (input = 1; input<5; input++) {
        cout << "Exam " << input << ": ";
        cin >> examarray[input];
        while (input < 0 || input > 100) {
            cout << "Error: Please enter a score from 0 - 100. " << endl;
            cout << "Exam " << input << ": ";
        }
    }
    double average = 0;
    int count = sizeof(examarray) / sizeof(int);
    for (size_t i = 0; i< count; ++i) {
        total += examarray[i]; }
    double = double / 4;

    return;

}



void printAllStudents(int &stu, &size)
{
    cout << "Index";
    cout << setw (10);
    cout << "ID";
    cout << setw (20);
    cout << "Name";
    cout << setw (15);
    cout << "Exam Grades";
    cout << setw (10);
    cout << "Average" << endl;
    while (int i=0; i<size; i++;) {
        printStudent(i);
    }
}


void printStudent(&StudentRecord) {
    cout << i;
    cout << setw (10);
    cout << StudentRecords[i].ID << setw (20);
    cout <<  StudentRecords[i].Name << setw (15);
    cout << StudentRecords[i].exam << setw (10);
    cout << StudentRecords[i].average << endl;
    cout << setprecision (2) << studentRecords[i].average << endl;
    return;
    }


void printSelectedStudent(StudentRecord &size) {
    int index;
    cout << "Enter the student's index number: ";
    cin >> index;
    if (index < 0 || index > size) {
        cout << "Error: Not a valid index number. ";
        return;
    }
    else
        printStudent(index);
    return;
}

void getAllStudents(StudentRecord &stu, &size) {
    cout << "Enter the total number of students: ";
    cin >> size;
    while (size < 1 || size > 32){
        cout << "Error: Enter a class size from 1 - 32" << endl;
        cout << "Enter the total number of students: ";
        cin >> size;
    }
    for int (i=0; i<size; i++) {
        getStudent(StudentRecord &stu);
    }
    return;
}





int main() {
    int StudentRecords[totalsize];
    int selection;
    int totalsize;
    cout << " ======================================== \n 1. Enter student data for the class \n 2. Print all student records \n 3. Print a single student record \n \n 0. Exit \n =======================================  \n Enter Selection: " << endl;
    cin >> selection;
        while (selection < 0 || selection > 3) {
            cout << "Error: Input one of the choices in the menu: \n";
            cin >> selection;
        }
        if (selection == 0)
            return 0;
        else if (selection == 1) {
            getAllStudents(StudentRecords, totalsize);
            totalsize++;
        }
        else if (selection == 2) {
            if (StudentRecords < 1) {
                cout << "Error: Enter a student record first. " << endl;
                return 0;
            }
            else {
            printAllStudents(StudentRecords);
             }
        }
        else if (selection == 3) {
            if (StudentRecords < 1) {
                cout << "Error: Enter a student record first. " << endl;
         }
            printSelectedStudent(StudentRecords);
        }
}



Errors:

l08.cpp: In function ‘void getStudent(StudentRecord&)’:
l08.cpp:20: error: ‘stu’ was not declared in this scope
l08.cpp:22: error: ‘strlen’ was not declared in this scope
l08.cpp:31: error: ‘strlen’ was not declared in this scope
l08.cpp:38: error: ‘examarray’ was not declared in this scope
l08.cpp:51: error: ‘total’ was not declared in this scope
l08.cpp:52: error: expected unqualified-id before ‘=’ token
l08.cpp: At global scope:
l08.cpp:60: error: expected identifier before ‘&’ token
l08.cpp: In function ‘void printAllStudents(int&, int&)’:
l08.cpp:71: error: expected `)' before ‘;’ token
l08.cpp:71: error: ‘i’ was not declared in this scope
l08.cpp:71: error: expected primary-expression before ‘)’ token
l08.cpp:71: error: expected `;' before ‘)’ token
l08.cpp: At global scope:
l08.cpp:77: error: variable or field ‘printStudent’ declared void
l08.cpp:77: error: expected primary-expression before ‘)’ token
l08.cpp: In function ‘void printSelectedStudent(StudentRecord&)’:
l08.cpp:93: error: no match for ‘operator>’ in ‘index > size’
l08.cpp:98: error: ‘printStudent’ was not declared in this scope
l08.cpp: At global scope:
l08.cpp:104: error: expected identifier before ‘&’ token
l08.cpp: In function ‘void getAllStudents(StudentRecord&, int&)’:
l08.cpp:112: error: expected `(' before ‘int’
l08.cpp:112: error: ‘i’ was not declared in this scope
l08.cpp:113: error: expected primary-expression before ‘&’ token
l08.cpp: In function ‘int main()’:
l08.cpp:123: error: ‘totalsize’ was not declared in this scope
l08.cpp:135: error: ‘StudentRecords’ was not declared in this scope
l08.cpp:139: error: ‘StudentRecords’ was not declared in this scope
l08.cpp:148: error: ‘StudentRecords’ was not declared in this scope
l08.cpp:151: error: ‘StudentRecords’ was not declared in this scope
The first error: you are using a variable 'stu' in your function; the function's parameter is called 'Stu' (note the capitalization). Perhaps this is a typo?

The second error: strlen is defined inside the <cstring> header. You should #include it. Maybe you should use the C++ string type std::string (in <string> header) anyway, though.

EDIT: All of the "was not declared in this scope" errors are due to the fact that you are usign a symbol (variable/function/etc name) which you have not declared. Make sure you declare (and initialize!) all the variables you use, and check for typos in variable names.
Last edited on
You've declared the parameter with upper case S and used it with lower case s.
1
2
3
4
void getStudent(StudentRecord &Stu) {
    string ID;
    cout << "Enter the student's ID number: ";
    cin >> stu.ID;


You need to include string.h for strlen.

examarray is an array of fixed lenth, 4. If you know that, you don't need to dynamically allocate it. In C and C++, an array index starts at zero, your code starts at 1.
1
2
3
4
5
6
    int input;
    examarray = new int [4];  // should be: char examarray[4];
    cout << "Please enter four exam scores: " << endl;
    for (input = 1; input<5; input++) { // shoud be: for (input = 0; input < 4; ++input)
        cout << "Exam " << input << ": ";
        cin >> examarray[input];


That should get you going. The error is telling you what's wrong and where.
Last edited on
Thank you for responding, although I'm afraid I still can't fix what was left. I tried making a few changes, but I'm still getting errors. Any more help would be greatly appreciated.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <vector>

using namespace std;

struct StudentRecord {
    char ID[10];
    string Name;
    int exam [4];
    double average;
};


void getStudent(StudentRecord &stu) {
    string ID;
    cout << "Enter the student's ID number: ";
    cin >> stu.ID;
    cin.ignore();
    while (strlen(stu.ID) != 10) {
        cout << "The Student ID must have 9 characters. " << endl;
        cout << "Enter the student's ID: ";
        cin >> stu.ID;
    }

    string Name;
    cout << "Enter the student's name. " << endl;
    getline (cin, stu.Name);
    while (strlen(stu.Name) < 1 || strlen(stu.Name) > 30) {
        cout << "Error: The student's name must be between 1 and 30 characters. " << endl;
        cout << "Enter the student's name: ";
        cin >> stu.Name;
    }

    int input;
    char examarray[4];
    cout << "Please enter four exam scores: " << endl;
    for (input = 0; input<4; input++) {
        cout << "Exam " << input << ": ";
        cin >> examarray[input];
        while (input < 0 || input > 100) {
            cout << "Error: Please enter a score from 0 - 100. " << endl;
            cout << "Exam " << input << ": ";
        }
    }
    double average = 0;
    int count = sizeof(examarray) / sizeof(int);
    for (size_t i = 0; i< count; ++i) {
        total += examarray[i]; }
    double = (double/4);

    return;

}



void printAllStudents(int &stu, &size)
{
    int i;
    cout << "Index";
    cout << setw (10);
    cout << "ID";
    cout << setw (20);
    cout << "Name";
    cout << setw (15);
    cout << "Exam Grades";
    cout << setw (10);
    cout << "Average" << endl;
    while (int i=0; i<size; i++;) {
        printStudent(i);
    }
}


void printStudent(StudentRecord &stu) {
    cout << i;
    cout << setw (10);
    cout << StudentRecords[i].ID << setw (20);
    cout <<  StudentRecords[i].Name << setw (15);
    cout << StudentRecords[i].exam << setw (10);
    cout << StudentRecords[i].average << endl;
    cout << setprecision (2) << studentRecords[i].average << endl;
    return;
    }


void printSelectedStudent(StudentRecord &stu, &size) {
    int index;
    cout << "Enter the student's index number: ";
    cin >> index;
    if (index < 0 || index > size) {
        cout << "Error: Not a valid index number. ";
        return;
    }
    else
        printStudent(&size);
    return;
}



void getAllStudents(StudentRecord &stu, &size){
    int i;
    cout << "Enter the total number of students: ";
    cin >> size;
    while (size < 1 || size > 32){
        cout << "Error: Enter a class size from 1 - 32" << endl;
        cout << "Enter the total number of students: ";
        cin >> size;
    }
    for (int i=0; i<size; i++) {
        getStudent(StudentRecord &stu);
    }
    return;
}





int main() {
    int totalsize;
    int StudentRecords[totalsize];
    int selection;
    cout << " ======================================== \n 1. Enter student data for the class \n 2. Print all student records \n 3. Print a single student record \n \n 0. Exit \n =======================================  \n Enter Selection: " << endl;
    cin >> selection;
        while (selection < 0 || selection > 3) {
            cout << "Error: Input one of the choices in the menu: \n";
            cin >> selection;
        }
        if (selection == 0)
            return 0;
        else if (selection == 1) {
            getAllStudents(StudentRecords, totalsize);
            totalsize++;
        }
        else if (selection == 2) {
            if (StudentRecords < 1) {
                cout << "Error: Enter a student record first. " << endl;
                return 0;
            }
            else {
            printAllStudents(StudentRecords);
             }
        }
        else if (selection == 3) {
            if (StudentRecords < 1) {
                cout << "Error: Enter a student record first. " << endl;
         }
            printSelectedStudent(StudentRecords);
        }
}






Errors:

l08.cpp: In function ‘void getStudent(StudentRecord&)’:
l08.cpp:31: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
l08.cpp:31: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
l08.cpp:51: error: ‘total’ was not declared in this scope
l08.cpp:52: error: expected unqualified-id before ‘=’ token
l08.cpp: At global scope:
l08.cpp:60: error: expected identifier before ‘&’ token
l08.cpp: In function ‘void printAllStudents(int&, int&)’:
l08.cpp:72: error: expected `)' before ‘;’ token
l08.cpp:72: error: expected primary-expression before ‘)’ token
l08.cpp:72: error: expected `;' before ‘)’ token
l08.cpp: In function ‘void printStudent(StudentRecord&)’:
l08.cpp:79: error: ‘i’ was not declared in this scope
l08.cpp:81: error: ‘StudentRecords’ was not declared in this scope
l08.cpp:85: error: ‘studentRecords’ was not declared in this scope
l08.cpp: At global scope:
l08.cpp:90: error: expected identifier before ‘&’ token
l08.cpp: In function ‘void printSelectedStudent(StudentRecord&, int&)’:
l08.cpp:99: error: invalid initialization of non-const reference of type ‘StudentRecord&’ from a temporary of type ‘int*’
l08.cpp:78: error: in passing argument 1 of ‘void printStudent(StudentRecord&)’
l08.cpp: At global scope:
l08.cpp:105: error: expected identifier before ‘&’ token
l08.cpp: In function ‘void getAllStudents(StudentRecord&, int&)’:
l08.cpp:115: error: expected primary-expression before ‘&’ token
l08.cpp: In function ‘int main()’:
l08.cpp:137: error: invalid initialization of non-const reference of type ‘StudentRecord&’ from a temporary of type ‘int*’
l08.cpp:105: error: in passing argument 1 of ‘void getAllStudents(StudentRecord&, int&)’
l08.cpp:141: error: ISO C++ forbids comparison between pointer and integer
l08.cpp:146: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int*’
l08.cpp:60: error: in passing argument 1 of ‘void printAllStudents(int&, int&)’
l08.cpp:150: error: ISO C++ forbids comparison between pointer and integer
l08.cpp:153: error: invalid initialization of non-const reference of type ‘StudentRecord&’ from a temporary of type ‘int*’
l08.cpp:90: error: in passing argument 1 of ‘void printSelectedStudent(StudentRecord&, int&)’
Last edited on
You're mixing strings. You're using C styles strings (a char array) and C++ strings.

The length of a C string is found with strlen.

The length of a C++ string is found with the .size method.

You can convert a C++ string to a C string with the .c_str() method.

l08.cpp:31: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
l08.cpp:31: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
These mean you should call .size() rather than strlen()
Topic archived. No new replies allowed.