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. The first, second, and third parts are correct but I'm having problems with all the parts after that. There is a bunch of code at the bottom in green that I wrote out, trying to figure out the five parts that are incomplete. If it's correct, where should it go? If it's incorrect, how should it look? I also don't really know what to do for the sixth, seventh, and eighth parts. And if it's not too much trouble, could you write out the code so I can see how it should look? If anyone is willing to help, it would be greatly appreciated.

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.

Seventh part:
Write a function that will remove a student from a "klass". The function should accept as parameter a "klass" and either a student, a first name, or an integer. The function should return a "klass".

Eighth part:
Write a series of functions that will serve as an interface for your functions. Do not modify your previously constructed functions.


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
#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
};

struct klass{
       student studentlist[30];// Array of students/max 30 students
       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
};

// Prototypes
void print(student, int);
student createstudent(char, char, int, int, int);
displaystudent(const, student);
addstudenttoclass(student, klass);
void print(student s[], int 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;
       } 

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++)
    {print(s, 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;
        */
    }

    system("PAUSE");
    return 0;
}
/*

// ***************************************************************************************************************** Incorrect
       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 >> "Student list: " << endl;
           cin >> student studentlist;
           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 << student studentlist[30] << endl; // Array of students/max 30 students
            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
        }

       student createstudent (char firstname[30], char lastname[30], int age, int studentnumber, int grade); // ??
       {
            cout << "First name of student: " << endl;
            cin >> firstname[30];
            cout << "Last name of student: " << endl;
            cin >> lastname[30];
            cout << "Age of student: " << endl;
            cin >> age;
            cout << "Student number: " << endl;
            cin >> studentnumber;
            cout << "Student grade: " << endl;
            cin >> grade;
            return createstudent; // ??
       }

       displaystudent (const student& student); // ??
       {
            student1.print();
       }

       addstudenttoclass (student1, klass1); // ??
       {
            // ??
       }

       struct klass1({}, 30, "ENG3U", "Smith", 1); // ??
       student student1=createstudent("Eric", "Cratman", 16, 803612, 10); // ??
       addstudenttoclass (student1, klass1); // ??
       for (student1:klass1.students); // ??
       {
           displaystudent(student1);
       }

// Part 6

// Part 7

// Part 8          
*/
Last edited on
First is using C-strings a requirement for this assignment? Or can you use C++ strings instead?

If C-strings are required your first structure looks adequate. However the second structure probably has some problems. First in your example for the fourth part you have: Example: addstudenttoclass (student1, class1); Note that "class1" has more than the 5 characters you allocated for the class name.

Now to the "third" part look at how you declared your function prototype for createstudent:
student createstudent(char, char, int, int, int);
Do you realize that the first two parameters will only accept a single character? Is that really what you want? Also I recommend, especially when you have several parameters of the same type, that you name the parameters. This will help document the function.

Something like:
student createstudent(char first_name, char last_name, int age, int student_number, int grade);
Now with a quick look you should be able to see that a single char for the first two parameters doesn't make much sense.

It also looks to me like you're not compiling often enough. You should be compiling often, many times after adding one line of code. Then you won't end up with a whole bunch of error messages scattered through a couple hundred lines of code.

That will probably help me with the third part but what about the other parts? Can I do anything with the code underneath that's written in green or do I get rid of it?
I didn't really look at your commented code. But you should work on one aspect of your program at a time. Get that to work properly before you move on to the next problem. Also some of the points I made in my post should be used in all of your program. For example naming the parameters in your function prototypes will help document your functions.

By the way you didn't answer my first question.
Sorry about that. I can use C++ strings. And I asked about the rest of my code because I wanted to get it done in one sitting, considering that I don't have access to C++ at home. I only have access in my Computer Studies class.
Last edited on
If you have a computer and an internet connection there are several good C++ compilers and IDEs available free for download.

Since you can use C++ strings I suggest you change your character strings to C++ strings, this will make things much simpler and much less error prone.

1
2
3
4
5
6
7
struct student{
       string lastname;
       string firstname;
       int age;
       int studentnumber;
       int grade; 
};


Even if the commented code is acceptable you still have a lot of functionality to complete. I still recommend you work on one part at a time. Get that one part working and throughly tested before moving to the next part.
But my prototype for "create student" is fine. When I compiled it there were no issues. The prototypes "display student" and "add student to class" are what is causing the errors. Because when I remove those and compile it, no errors present themselves. And using char instead of string works well with what I have. It has caused me not issues and I have used the same technique in other smaller projects.
But my prototype for "create student" is fine.

If you say so. You don't seem to understand the difference between a single character and a C-string, but I guess that doesn't matter because it works for you. You also don't seem to understand one of the biggest problems with C-strings, buffer overrun errors that do not happen with C++ strings. But if it works for you I'll leave you to it.

If my code was working, I wouldn't have posted it and asked for help. I set it as char and put a max of 30 characters which does work, as seen in my structure for "student" and "klass". I have used this multiple times and it works. I asked my teacher if I am allowed to do this and he said it can work either with string or char with a set amount of characters, which in my case is 30 characters. It is also in the commented part of the code as well. It may seem unorthodox but it is still a way to do it. But the "third" part was not my biggest issue. It was the parts after that.
Last edited on
Topic archived. No new replies allowed.