FatBoyTim
asked on
c++ class, creating an object using a constructor
hi, i have a program which reads in files and does various things with them. it doesn't quite work yet because whenever i try to run the file i get "Segmentation Fault (core dumped)" error so i cant test anything else. I cant see what's wrong with the program so if somebody could help me that would be superb. cheers,
tim
C++ Code, the error is in the first line of 'main' - creating the object:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
struct Player {
char* id;
char* name;
double ranking;
};
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
using namespace std;
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
class matches {
public:
matches();
~matches();
// int max_name_length;
// int noOfPlayers;
bool isStatement(char test[]);
bool isID(char test[]);
char * LTrim (char trim[]);
char * RTrim (char trim[]);
char * Trim (char trim[]);
char * RemoveSpaces (char trim[]);
Player selectDetails(Player player, char test[], int testLength);
void doWork();
// char * participants;
};
matches::matches() {
}
matches::~matches() {
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
bool matches::isStatement(char test[]) {
for (int x=0; x<strlen(test); x++) {
if (test[x] != ' ') {
if (test[x] == '/' && test[x+1] == '/') {
return true;
} else {
return false;
} // end if-else
} // end if
} // end for
} // end function: isStatement
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
bool matches::isID(char test[]) {
// Get ascii value from char to check for alpha-numeric value
int ascOne = int(test[0]);
// Check the first character is a capital letter
if (ascOne < 91 && ascOne > 64) {
// Check the last character is a number
if (test[1] < 58 && test[1] > 47) {
return true;
} else {
return false;
} // end if-else
} else {
return false;
}// end if-else
} // end function: checkForID
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
char* matches::LTrim (char trim[]) {
do {
if (trim[0] != ' ') {
return trim;
} else {
for (int x = 0; x < strlen(trim)-1; x++) {
trim[x] = trim[x+1];
}
trim[strlen(trim)-1] = '\0';
}
} while (trim[0] == ' ');
return trim;
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
char* matches::RTrim (char trim[]) {
do {
if (trim[strlen(trim)-1] != ' ') {
return trim;
} else {
trim[strlen(trim)-1] = '\0';
}
} while (trim[strlen(trim)-1] == ' ');
return trim;
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
char* matches::Trim (char trim[]) {
return LTrim(RTrim(trim));
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
char* matches::RemoveSpaces (char trim[]) {
for (int x = 0; x < strlen(trim); x++) {
if (trim[x] == ' ') {
do {
for (int y = x; y < strlen(trim)-1; y++) {
trim[y] = trim[y+1];
} // end for
trim[strlen(trim)-1] = '\0';
} while (trim[x] == ' ');
} // end if
} // end for
return LTrim(RTrim(trim));
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
Player matches::selectDetails(Pla yer player, char test[], int testLength) {
// Declare local variables
int strPos=0, strLen=0, tracker=0;
char *temp;
player.ranking = 0;
////////////////////
///Read in the id///
////////////////////
// Skip initial spacing
do { strPos++; } while (test[strPos] == ' ');
// Work out length of text
strLen = strPos;
do { strLen++; } while (test[strLen] != ' ');
strLen -= strPos;
// Declare struct array length using DMA
player.id = new char[strLen];
// Read all the characters until a space is reached
tracker = 0;
do {
player.id[tracker] = test[strPos];
strPos++;
tracker++;
} while (tracker != strLen);
player.id[strLen] = '\0';
//////////////////////
///Read in the name///
//////////////////////
// Skip next spacing
do { strPos++; } while (test[strPos] == ' ');
// Work out length of text
strLen = strPos;
do { strLen++; } while (test[strLen] != ' ');
strLen -= strPos;
// Declare struct array length using DMA
player.name = new char[strLen];
// Read all the characters until a space is reached
tracker = 0;
do {
player.name[tracker] = test[strPos];
strPos++;
tracker++;
} while (tracker != strLen);
player.name[strLen] = '\0';
/////////////////////////
///Read in the ranking///
/////////////////////////
// Skip next spacing
do { strPos++; } while (test[strPos] == ' ');
// Work out length of text
strLen = strPos;
do { strLen++; } while (strLen != strlen(test));
strLen -= strPos;
// Declare struct array length using DMA
temp = new char[strLen];
// Read all the characters until a space is reached
tracker = 0;
do {
temp[tracker] = test[strPos];
strPos++;
tracker++;
} while (tracker != strLen);
temp[strLen] = '\0';
// The array 'temp' now contains the characters of the ranking. We need to convert them to an integer
for (int n = 0; n < strlen(temp); n++) {
player.ranking += (temp[n]-48) * pow(10,strlen(temp)-n-1);
} // end for
return player;
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
void matches::doWork () {
// Declare variables
int counter = 0;
char buffer[256];
char participants[256];
Player *players;
// Open file streams
ifstream playerfile ("players.dat");
ifstream participantsfile ("participants.dat");
// Error handling for file-opening
if (!playerfile.is_open()) { cout << "Error opening player file"; exit(1); } // end if
if (!participantsfile.is_open ()) { cout << "Error opening participants file"; exit(1); } // end if
// Read in the participants
do {
participantsfile.getline (buffer, 100, '\n');
if (!isStatement(buffer)) {
int noOfPlayers = strlen(RemoveSpaces(buffer ));
participants = buffer;
players = new Player[noOfPlayers];
for (int n = 0; n < noOfPlayers*2; n+2) {
players[n/2].id[0] = participants[0];
players[n/2].id[1] = participants[1];
}
} // end if
cout << participants;
} while (! participantsfile.eof());
cout << participants << endl;
for (int n = 0; n < strlen(participants); n++) {
cout << "HELLO";
cout << participants[n] << buffer[n] << endl;
} // end for
//Start searching the file for player details
do {
//Read in one line at a time
playerfile.getline (buffer, 100);
// If the line is not a statement (//) then call function to get player details
if (!isStatement(buffer)) {
// player[counter] = selectDetails(player[count er], buffer, strlen(buffer));
// cout << "Name: " << player[counter].name << endl;
// cout << "ID: " << player[counter].id << endl;
// cout << "Ranking: " << player[counter].ranking << endl;
// cout << endl;
}
} while (! playerfile.eof()); // end while
// Close the file streams
participantsfile.close();
playerfile.close();
// Print out the array of players and their details
for (int n = 0; n <25; n++ ) {
// cout << player[n].id;
}
}
/*------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------*/
int main() {
matches details;
details.doWork();
return 0;
} // end function: main
tim
C++ Code, the error is in the first line of 'main' - creating the object:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
/*------------------------
struct Player {
char* id;
char* name;
double ranking;
};
/*------------------------
using namespace std;
/*------------------------
class matches {
public:
matches();
~matches();
// int max_name_length;
// int noOfPlayers;
bool isStatement(char test[]);
bool isID(char test[]);
char * LTrim (char trim[]);
char * RTrim (char trim[]);
char * Trim (char trim[]);
char * RemoveSpaces (char trim[]);
Player selectDetails(Player player, char test[], int testLength);
void doWork();
// char * participants;
};
matches::matches() {
}
matches::~matches() {
}
/*------------------------
bool matches::isStatement(char test[]) {
for (int x=0; x<strlen(test); x++) {
if (test[x] != ' ') {
if (test[x] == '/' && test[x+1] == '/') {
return true;
} else {
return false;
} // end if-else
} // end if
} // end for
} // end function: isStatement
/*------------------------
bool matches::isID(char test[]) {
// Get ascii value from char to check for alpha-numeric value
int ascOne = int(test[0]);
// Check the first character is a capital letter
if (ascOne < 91 && ascOne > 64) {
// Check the last character is a number
if (test[1] < 58 && test[1] > 47) {
return true;
} else {
return false;
} // end if-else
} else {
return false;
}// end if-else
} // end function: checkForID
/*------------------------
char* matches::LTrim (char trim[]) {
do {
if (trim[0] != ' ') {
return trim;
} else {
for (int x = 0; x < strlen(trim)-1; x++) {
trim[x] = trim[x+1];
}
trim[strlen(trim)-1] = '\0';
}
} while (trim[0] == ' ');
return trim;
}
/*------------------------
char* matches::RTrim (char trim[]) {
do {
if (trim[strlen(trim)-1] != ' ') {
return trim;
} else {
trim[strlen(trim)-1] = '\0';
}
} while (trim[strlen(trim)-1] == ' ');
return trim;
}
/*------------------------
char* matches::Trim (char trim[]) {
return LTrim(RTrim(trim));
}
/*------------------------
char* matches::RemoveSpaces (char trim[]) {
for (int x = 0; x < strlen(trim); x++) {
if (trim[x] == ' ') {
do {
for (int y = x; y < strlen(trim)-1; y++) {
trim[y] = trim[y+1];
} // end for
trim[strlen(trim)-1] = '\0';
} while (trim[x] == ' ');
} // end if
} // end for
return LTrim(RTrim(trim));
}
/*------------------------
Player matches::selectDetails(Pla
// Declare local variables
int strPos=0, strLen=0, tracker=0;
char *temp;
player.ranking = 0;
////////////////////
///Read in the id///
////////////////////
// Skip initial spacing
do { strPos++; } while (test[strPos] == ' ');
// Work out length of text
strLen = strPos;
do { strLen++; } while (test[strLen] != ' ');
strLen -= strPos;
// Declare struct array length using DMA
player.id = new char[strLen];
// Read all the characters until a space is reached
tracker = 0;
do {
player.id[tracker] = test[strPos];
strPos++;
tracker++;
} while (tracker != strLen);
player.id[strLen] = '\0';
//////////////////////
///Read in the name///
//////////////////////
// Skip next spacing
do { strPos++; } while (test[strPos] == ' ');
// Work out length of text
strLen = strPos;
do { strLen++; } while (test[strLen] != ' ');
strLen -= strPos;
// Declare struct array length using DMA
player.name = new char[strLen];
// Read all the characters until a space is reached
tracker = 0;
do {
player.name[tracker] = test[strPos];
strPos++;
tracker++;
} while (tracker != strLen);
player.name[strLen] = '\0';
/////////////////////////
///Read in the ranking///
/////////////////////////
// Skip next spacing
do { strPos++; } while (test[strPos] == ' ');
// Work out length of text
strLen = strPos;
do { strLen++; } while (strLen != strlen(test));
strLen -= strPos;
// Declare struct array length using DMA
temp = new char[strLen];
// Read all the characters until a space is reached
tracker = 0;
do {
temp[tracker] = test[strPos];
strPos++;
tracker++;
} while (tracker != strLen);
temp[strLen] = '\0';
// The array 'temp' now contains the characters of the ranking. We need to convert them to an integer
for (int n = 0; n < strlen(temp); n++) {
player.ranking += (temp[n]-48) * pow(10,strlen(temp)-n-1);
} // end for
return player;
}
/*------------------------
void matches::doWork () {
// Declare variables
int counter = 0;
char buffer[256];
char participants[256];
Player *players;
// Open file streams
ifstream playerfile ("players.dat");
ifstream participantsfile ("participants.dat");
// Error handling for file-opening
if (!playerfile.is_open()) { cout << "Error opening player file"; exit(1); } // end if
if (!participantsfile.is_open
// Read in the participants
do {
participantsfile.getline (buffer, 100, '\n');
if (!isStatement(buffer)) {
int noOfPlayers = strlen(RemoveSpaces(buffer
participants = buffer;
players = new Player[noOfPlayers];
for (int n = 0; n < noOfPlayers*2; n+2) {
players[n/2].id[0] = participants[0];
players[n/2].id[1] = participants[1];
}
} // end if
cout << participants;
} while (! participantsfile.eof());
cout << participants << endl;
for (int n = 0; n < strlen(participants); n++) {
cout << "HELLO";
cout << participants[n] << buffer[n] << endl;
} // end for
//Start searching the file for player details
do {
//Read in one line at a time
playerfile.getline (buffer, 100);
// If the line is not a statement (//) then call function to get player details
if (!isStatement(buffer)) {
// player[counter] = selectDetails(player[count
// cout << "Name: " << player[counter].name << endl;
// cout << "ID: " << player[counter].id << endl;
// cout << "Ranking: " << player[counter].ranking << endl;
// cout << endl;
}
} while (! playerfile.eof()); // end while
// Close the file streams
participantsfile.close();
playerfile.close();
// Print out the array of players and their details
for (int n = 0; n <25; n++ ) {
// cout << player[n].id;
}
}
/*------------------------
int main() {
matches details;
details.doWork();
return 0;
} // end function: main
what compiler are you using
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ilikenine's comment is good, but if you want to continue using your pointer-declaration, then after writing:
players = new Player[noOfPlayers];
write:
for ( i = 0 ; i < noOfPlayers ; i ++ )
{
players[i].id = new char[<whatever-size>] ;
players[i].name = new char[<whatever-size>] ;
} // end for
AND OH!
CHECK THE UPDATION OF YOUR FOR LOOP : you've written simply 'n + 2' --> should it not be 'n = n + 2' or 'n += 2' ????
Mayank.
players = new Player[noOfPlayers];
write:
for ( i = 0 ; i < noOfPlayers ; i ++ )
{
players[i].id = new char[<whatever-size>] ;
players[i].name = new char[<whatever-size>] ;
} // end for
AND OH!
CHECK THE UPDATION OF YOUR FOR LOOP : you've written simply 'n + 2' --> should it not be 'n = n + 2' or 'n += 2' ????
Mayank.
ASKER
cheers for that. i know the problem was with the constructor and i spent a lot of time trying to find it. i guess i found it and fixed it, but then posted the message with a different error :-P thanks for your help everyone, im starting to understand memory allocation quite well now!
fbt
fbt