C++ Structures

Sorry this is so long but I'm really stuck. Some parts of my code are not working and I don't know why. There is also a requirement in the second part that I am confused on. It asks for an "Array of students". I'm not sure what to do there. I'm also having difficulties with the sixth part of the program. If anyone is willing to help, it would be greatly appreciated. (If you need to rewrite my code, I'm fine with that if it helps to solve the problems.)

First part:
Create a structure type called "Student" with the following properties
-first name
-last name
-age
-student number
-grade

Second part:
Create a structure type called "klass" with the following properties
-array of students
-size of class
-name of class
-last name of teacher
-period of the day

Third part:
Create a function called "create student" which takes as parameters all the characteristics of a student. It should return a student with the appropriate values.
Example: student1 ("Eric", "Cartman", 16, 803612, 10);

Forth part:
Create a function called "add student to class" which accepts as parameters a student and a class. The function should add the student to the array of student of the specific class.
Example: addstudenttoclass (student1, class1);

Fifth part:
Write a function called "display student" that accepts a student as parameters. This function should display the properties of the student.

Sixth part:
Write a function that searches a class for a given student. The function should accept a string representing the student's last name and return an integer representing in the class's array where the student is stored. If the student is not in the class it should return a value of -1.

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
 
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

struct student{
       char lastname[30]; // Student's first name/max 30 characters
       char firstname[30]; // Student's first name/max 30 characters
       int age; // Student's age
       int studentnumber; // Student's ID number
       int grade; // Student's grade
};

int main()
{
    student s[1]; // s of type student
    int x;

    cout << "Enter information of student: " << endl;

    for (int x=0; x<1; x++)
    {
        cout << "Enter first name: " << endl;
        cin >> s[x].firstname; // Takes student's first name
        cout << "Enter last name: " << endl;
        cin >> s[x].lastname; // Takes student's last name
        cout << "Enter age: " << endl;
        cin >> s[x].age; // Takes student's age
        cout << "Enter student number: " << endl;
        cin >> s[x].studentnumber; // Takes student's ID number
        cout << "Enter grade: " << endl;
        cin >> s[x].grade; // Takes student's grade
        cout << endl;
    }

    cout << "Information of student" << endl << endl; // Displays info on student

    for (int x=0; x<1; x++)
    {
        cout << "First name: " << s[x].firstname << endl; // Displays first name
        cout << "Last name: " << s[x].lastname << endl; // Displays last name
        cout << "Age of student: " << s[x].age << endl; // Displays age
        cout << "Student number: " << s[x].studentnumber << endl; // Displays ID number
        cout << "Student grade: " << s[x].grade << endl; // Displays grade
        cout << endl;
    }    

struct klass{
       char arrayofstudents;
       int size; // Size of class
       int periodoftheday; // Period of the day the class is in
       char nameofclass[5]; // Name of class/max 5 characters
       char lastnameofteacher[30]; // Last name of teacher/max 30 characters
};

       klass k[1]; // k of type klass
       int y;

       cout << "Enter information of class: " << endl; // Displays info on class

       for (int y=0; y<1; y++)
       {
           cout << " " << endl;
           cin >> vector<student>students;
           cout << "Enter size of class: " << endl;
           cin >> k[y].size; // Takes size of class
           cout << "Enter period of the day: " << endl;
           cin >> k[y].periodoftheday; // Takes period of the day the class is in
           cout << "Enter name of class: " << endl;
           cin >> k[y].nameofclass[5]; // Takes the name of the class/max 5 characters
           cout << "Enter last name of teacher: " << endl;
           cin >> k[y].lastnameofteacher[30]; // Takes last name of teacher/max 30 characters
           cout << endl;
        }

        cout << "Information of class" << endl << endl; // Displays info on class

        for (int y=0; y<1; y++)
        {
            cout << " " << endl;
            cout << "Size of class: " << k[y].size << endl; // Displays size of class
            cout << "Period of the day: " << k[y].periodoftheday << endl; // Displays period of the day
            cout << "Name of class: " << k[y].nameofclass[5] << endl; // Displays name of class
            cout << "Last name of teacher: " << k[y].lastnameofteacher[30]; // Displays last name of teacher
        }

struct student{
       char firstname[30];
       char lastname[30];
       int age;
       int studentnumber;
       int grade;
       void print() const
       {
            cout << "First name: " << firstname << endl;
            cout << "Last name: " << lastname << endl;
            cout << "Age for student: " << age << endl;
            cout << "Student number: " << studentnumber << endl;
            cout << "Student grade: " << grade << endl;
       }
       };

       int createstudent (char firstname[30], char lastname[30], int age, int studentnumber, int grade)
       {
            return {firstname[30], lastname[30], age, studentnumber, grade};
       }

       void displaystudent (const student& student)
       {
            student.print();
       }

       struct klass{
              vector<student>student;
              int size;
              int periodoftheday;
              char nameofclass[5];
              char lastnameofteacher[30];
       };

       void addstudenttoclass (student& s, klass& c)
       {
            c.student.push_back(s);
            c.size=c.student.size();
       }

       klass klass1{{}, 0, "ENG3U", "Smith", 1};
       student student1=createstudent("Eric", "Cratman", 16, 803612, 10);
       addstudenttoclass(student1, klass1);
       for (auto& student:klass1.students){
           displaystudent(student);
       }          

    system("PAUSE");
    return 0;
}
First, put your class declarations at the top, before main.

Second, use your second definition of struct student (delete the first one).

Third, I think it would be less confusing if you change your program in main to have its own copy of firstname, lastname, age, etc. and have the user enter the appropriate values into those variables, then call your createstudent() function and pass those variables.

Now for your specific questions:

There is also a requirement in the second part that I am confused on. It asks for an "Array of students". I'm not sure what to do there.


Your professor wants you to include an array (or vector) of your student class.

I'm also having difficulties with the sixth part of the program.


Just loop through your array (or vector) of students to find a match. If you find a match, return the index in your array (or vector) where you found the student. If not found, return -1.



Lines 50-56: Style issue. Keep your structure declarations together at the beginning of the program.

line 66: You can't cin to a vector.

Line 89-103: Why do you have another declaration of student here?

Line 105: createstudent says it returns an int. What do you think you;re returning on line 107?

Lines 105-113,123-127: You can't declare nested functions inside main.

Line 115-121: Why do you have another declaration of klass here?

Line 130: You can only assign another student to student1. You can't assign an int to it, which is what createstudent returns.

Line 50,115: Your two declarations of klass don;t match.
Line 51: You're trying to declare arrayofstudents as a char.

Lines 72:74,85,86: You're trying to output a single out of bounds character.


Topic archived. No new replies allowed.