Reputation: 61
So I have an exercise in C++. I have a structure student with the following members: 1 int, 2 char, 1 float and (I write the lines from the exercise): "two pointers on functions for reading data void (read)(studentst) and one pointer for displaying data void (write)(studentst) "
I have to read from keyboard the number of students and then to allocate memory for an array with students.
Then I have to define 2 functions: void readData(student* st) and void writeData(student* st) which read and display the members of one student structure. I have to go through students array and to initialize the pointers to functions void (read)(studentst) and void (write)(studentst) with void readData(student* st) and void writeData(student* st). Here I am a little bit confused.
The last thing I need to do is to go through students array and to call the functions v[i].read(&v[i]) and v[i].write(&v[i]) to read and display data.
Here is my code:
HEADER
#pragma once
struct student {
int nrID;
char name[100];
char gender[20];
float mark;
void(*read)(student* st);
void(*write)(student* st);
};
void readStudents(int n);
void readData(student* st);
void writeData(student* st);
FUNCTIONS FILE
#include <iostream>
#include "header.h"
using namespace std;
void readStudents(int n) {
cout << "Number of students: ";
cin >> n;
student *v;
v = new student[n]; // here I allocate the students array
/*
for(int i=0; i<n; ++i) {
v[i].read(??) = readData(&v[i]);
v[i].write(??) = writeData(&v[i]);
}
*/ // Here I am confused. I'm not sure if I initialized the pointers to function in a right way.
for (int i = 0; i<n; ++i) {
v[i].read(&v[i]);
v[i].write(&v[i]);
}
}
// below I created 2 functions which read and display the members of one student struct.
void readData(student* st) {
cout << "Enter the nrId: ";
cin >> st->nrID;
cout << "Enter the name: ";
cin >> st->name;
cout << "Enter the gender: ";
cin >> st->gender;
cout << "Enter the mark: ";
cin >> st->mark;
}
void writeData(student* st) {
cout << "Id number: " << st->nrID << endl;
cout << "Name: " << st->name << endl;
cout << "Gender: " << st->gender << endl;
cout << "Mark: " << st->mark << endl;
}
MAIN FILE
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
int n;
readStudents(n);
return 0;
}
I compile the code in Code Block and all I have is "Number of students:" the the program crashes. How to fix those pointers initializations?
Upvotes: 0
Views: 344
Reputation: 206697
Your program has undefined behavior since the read
and write
members of the struct
have not been assigned to anything valid.
It's not clear to me why read
and write
are non-static
members of the struct
. It will be better to make them static
members, assign appropriate functions to them, and then use them to read/write instance of the struct
.
struct student {
int nrID;
char name[100];
char gender[20];
float mark;
// make these static members
static void(*read)(student* st);
static void(*write)(student* st);
};
Make sure to update .cpp file by adding.
void (*student::read)(student* st) = nullptr;
void (*student::write)(student* st) = nullptr;
Update main
int main()
{
// Assign functions to read/write
student::read = readData;
student::write = writeData;
int n;
readStudents(n);
return 0;
}
Upvotes: 1