Problems Creating an Array of a Custom Class
Jun 27, 2011 at 11:00pm UTC
Ok, im writing a custom class that uses two c-style strings and a double. Everything complies and seems to run correctly, until one takes a closer look. From what I've been able to tell, the Card custom class does not know how to make an array of itself, but my constructor seems to be coded correctly, any help would be awesome.
class.h
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
#include "equipment.h"
#include <cstring>
#include <iostream>
#ifndef CLASS_H_
#define CLASS_H_
using namespace std;
class Card
{
private :
char * RFID;
double amntAvailable;
char * customerName;
public :
// Default Constructor
Card(){
RFID = new char [20]; //Important: must be changed to increase length of RFID;
amntAvailable = 0;
customerName = new char [50]; // Important: must be changed to increase amount of space available;
}
// Constructor with Arguments
Card(char * ID, double initial, char * a){
RFID = new char [20];
customerName = new char [50];
RFID = ID;
amntAvailable = initial;
customerName = a;
}
// Copy Constructor
Card(const Card &other)
{
RFID = new char [20];
customerName = new char [50];
strcpy(RFID, other.RFID);
amntAvailable = other.amntAvailable;
strcpy(customerName, other.customerName);
}
// Destructor
~Card(){
delete [] RFID;
delete [] customerName;
}
// Overloaded assignment operator
Card operator = (const Card& other){
if (this != &other)
{
delete [] RFID;
delete [] customerName;
amntAvailable = other.amntAvailable;
RFID = new char [20];
customerName = new char [50];
strcpy(RFID, other.RFID);
strcpy(customerName, other.customerName);
}
return *this ;
}
char * getRFID(){
return RFID;
}
char * getName(){
return customerName;
}
void setBal(double other){
amntAvailable = other;
}
void setName(char * other){
customerName = other;
}
void setRFID(char * other){
RFID = other;
}
double checkBal(){
return amntAvailable;
}
bool raiseBal(int amnt){
if (amntAvailable += amnt)
return true ;
else
return false ;
}
bool reduceBal(double amnt){
if (amntAvailable >= amnt)
{
amntAvailable -= amnt;
return true ;
}
else
return false ;
}
friend ostream& operator <<(ostream&, Card&);
friend int rfidSearch(Card[] , char *, int );
};
#endif /*CLASS_H_*/
main.cpp
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
//#include <stdio.h>
#include "class.h"
#include "equipment.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int nameLength = 10; //Dictates the length of the name.
const int rfidLength = 10; //Dictates how long rfid char* is, can easily be changed to accomadate more info.
// Allows for sending of data using the extraction operator
ostream& operator <<(ostream& out, Card& other){
char * rfid;
rfid = new char [20];
char * name;
name = new char [20];
double balance = 0;
rfid = other.getRFID();
for (int i =0; i < strlen(rfid); i ++)
{
out << rfid[i];
}
balance = other.checkBal();
out << " " << balance << " " ;
name = other.getName();
for (int i = 0; i <strlen(name); i ++)
{
out << name[i];
}
out << endl;
delete [] rfid;
delete [] name;
return out;
}
int rfidSearch( Card table[], char * goal, int size)
{
int count = 0;
for (int i = 0; i < size; i ++)
{
for (int j = 0; j <= strlen(goal); j ++)
{
}
}
return count;
}
int main()
{
bool done = false ;
char * input; //Receives the card number
char * temp1;
char * temp2; // Holds the information before it is stored in card database.
double holder;
char c; // Gets 1 letter at a time from account database.
ifstream inputFile;
int length = 0;
int size = 0;
int count = 0;
Card* data;
inputFile.open("accounts.txt" , ifstream :: in);
inputFile.seekg(0L, ios :: end);
length = inputFile.tellg();
inputFile.seekg(0L, ios :: beg);
size = length/(sizeof (char )*rfidLength+sizeof (char )*nameLength+sizeof (double ));
data = new Card[size];
temp1 = new char [rfidLength];
temp2 = new char [nameLength];
for (int i = 0; i < size; i ++)
{
inputFile.get(temp1 , 100, ' ' );
data[i].setRFID(temp1);
//for(int j =0; j < strlen(temp1); j++)
// cout << temp1[j];
inputFile >> holder;
data[i].setBal(holder);
//cout << " " << holder << endl;
inputFile.get(temp2, 100);
data[i].setName(temp2);
//for(int j =0; j < strlen(temp2); j++)
// cout << temp2[j];
//cout << endl;
}
// Just ignore this loop, no need for it yet.
while (!done)
{
input = new char [rfidLength];
done = true ;
}
delete [] input;
return 0;
}
and here is accounts.txt , where the information that should be going in the array of Cards is coming from:
0010508538 10.00 Generic nAme
0010500654 10.00 Generic naMe
0010509230 10.00 Generic namE
0010454196 10.00 Generic NamE
Jun 27, 2011 at 11:09pm UTC
Quick note, just ignore equipment.h, hasn't been fully flesh out yet
Jun 27, 2011 at 11:28pm UTC
In terms of your use of
char *
, in most cases you set aside the memory but then you just assign it from another
char *
. This is incorrect. It amounts to a shallow copy of the pointer. You assign the
char *
correctly here (with the use of
strcpy
):
1 2 3 4 5 6 7 8 9 10 11 12
// Copy Constructor
Card(const Card &other)
{
RFID = new char [20];
customerName = new char [50];
strcpy(RFID, other.RFID);
amntAvailable = other.amntAvailable;
strcpy(customerName, other.customerName);
}
However, instead of hard-coding the lengths to 20 and 50, you should use
strlen
. You should go over your code and where ever you see a straight assignment to a dynamic
char *
, you should use the method you've used in the copy constructor instead.
Last edited on Jun 27, 2011 at 11:29pm UTC
Jun 28, 2011 at 12:39am UTC
cool, I'll change over all that stuff and see if that fixes it.
Jun 28, 2011 at 12:45am UTC
You are an absolute Life saver dude, thank you so much, I will always come back to this site, been rangling with this damn code for 3 days and wasn't able to figure it out.
Topic archived. No new replies allowed.