jeffw362 17 Newbie Poster

Hi, I'm having an issue in my program when I try to call delete in my destructor.
Can someone take a look and see what I doing wrong? I've looked it over and over but I just can't figure out what's wrong.

destructor code:

StudentRecord::~StudentRecord(){		// Destructor
	delete[] m_firstName;
	delete[] m_lastName;
}

and this is my constructor code:

StudentRecord::StudentRecord(char* first,char* last,unsigned int id,float mark){	// Overloaded constructor
	m_firstName = strcpy(new char[strlen(first)+1],first);
	m_lastName = strcpy(new char[strlen(last)+1],last);
	m_id = id;
	m_mark = mark;
}

Here is the full code for my project in case it's needed.

StudentRecord.h

#ifndef STUDENTRECORD_H
#define STUDENTRECORD_H
class StudentRecord{
	char* m_firstName;
	char* m_lastName;
	unsigned int m_id;
	float m_mark;
public:
	StudentRecord();
	StudentRecord(char*,char*,unsigned int,float);
	StudentRecord(StudentRecord&);
	~StudentRecord();
	StudentRecord& operator=(StudentRecord&);
	bool operator>(StudentRecord&);
	bool operator<(StudentRecord&);
	void Report();
	char* GetFirstName(){return m_firstName;}
	char* GetLastName(){return m_lastName;}
	unsigned int GetID(){return m_id;}
	float GetMark(){return m_mark;}
};
#endif

StudentRecord.cpp

#include <iostream>
#include "StudentRecord.h"
using namespace std;
StudentRecord::StudentRecord(){			// Default constructor

	m_firstName = NULL;
	m_lastName = NULL;
	m_id = 0;
	m_mark = 0.00;
}
StudentRecord::StudentRecord(char* first,char* last,unsigned int id,float mark){	// Overloaded constructor
	m_firstName = strcpy(new char[strlen(first)+1],first);
	m_lastName = strcpy(new char[strlen(last)+1],last);
	m_id = id;
	m_mark = mark;
}
StudentRecord::StudentRecord(StudentRecord& ref){		// Copy constructor
	*this = ref;
}
StudentRecord::~StudentRecord(){		// Destructor
	delete[] m_firstName;
	delete[] m_lastName;
}
StudentRecord& StudentRecord::operator=(StudentRecord& ref){	// Overloaded = operator
	m_firstName = ref.m_firstName;
	m_lastName = ref.m_lastName;
	m_id = ref.m_id;
	m_mark = ref.m_mark;

	return *this;
}
bool StudentRecord::operator>(StudentRecord& ref){		// Overloaded > operator
	if(m_mark > ref.m_mark)
		return true;
	else
		return false;
} …
jeffw362 17 Newbie Poster

Does anyone have any ideas?

jeffw362 17 Newbie Poster

I'm using fgets not scanf. I get a line from the file and then use strtok to split the line. Then I use a switch statement to assign the values to the members of my struct.

WaltP commented: Good. fgets() is the right way to do it... +17
jeffw362 17 Newbie Poster

that printf was to make sure that I was getting one line at a time from the file and that it was the correct information before splitting it up. When I assign a new first and last name from the file, it assigns it to the previous strings as well. I thought that maybe char* firstname and char* lastname are pointing to the same memory for each struct but I call malloc() each time.

jeffw362 17 Newbie Poster

Oh and here is my ListRecords function:

void ListRecords(void){
	int i;
	for(i=0;i<g_numRecords;i++){
		printf("Record #%d\nFirst Name = %s\nLast Name = %s\nID = %d\nMark = %f\n\n",
				i+1,
				g_ppRecords[i]->firstName,
				g_ppRecords[i]->lastName,
				g_ppRecords[i]->id,
				g_ppRecords[i]->mark);
	}
}
jeffw362 17 Newbie Poster

this is my output.

OPEN FILE
Please enter the name of the file to open: 001.txt

FileName: 001.txt
1. List records in memory
2. Add a new record
3. Delete first record
4. Quit and save records to file
1
Record #1
First Name = Adam
Last Name = Smith
ID = 1
Mark = 88.500000

Record #2
First Name = Adam
Last Name = Smith
ID = 2
Mark = 73.699997 

Record #3
First Name = Adam
Last Name = Smith
ID = 3
Mark = 12.500000

1. List records in memory
2. Add a new record
3. Delete first record
4. Quit and save records to file

I removed printf("HERE %d:%s\n",g_numRecords,input);, it was just for testing purposes. My input file has the following:
Jeff Watts 1 88.5
Toby Jefferson 2 73.7
Adam Smith 3 12.5

jeffw362 17 Newbie Poster

I'm writing a program that takes records(first and last name,id and mark), reads them from a file and inserts the information in StudentRecord structures. For some reason,when I go to list the information, the first and last name strings in each structures are all the same. The first and last names are the last ones loaded from the file. I tried printing the members right after they're assigned and it's the right name but when I call the list function the names aren't right but the ID and mark data is correct. For the list function all I do at the moment is print each member in all the structures. For example I could have the following in a file:
FName1 LName1 1 99.9
FName2 LName2 2 54.9
FName3 LName3 3 69.9
FName4 LName4 4 57.9

The output for my list function ends up being:
FName4 LName4 1 99.9
FName4 LName4 2 54.9
FName4 LName4 3 69.9
FName4 LName4 4 57.9

Here is my code, the openfile function was written by me, all the rest was provided to me and cannot be changed whatsoever. Thank you in advanced for any help you can give me.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct StudentRecord{
	char* firstName;
	char* lastName;
	int id;
	float mark;
}StudentRecord;

StudentRecord** g_ppRecords;	/* array of pointers to StudentRecords */
int g_numRecords = 0;			/* how many records */
char* g_FileName;				/* file to open */
FILE* …