I understand that classes are blueprints, and instances of this class/blueprint can be created at will with different names, each instance uses the properties and methods of the class, but they can be set different values. My question is, 'Can I create instances of a class, but store each instance in an array?.' OR... 'Can I create an array that basically is the class, so each cell of the table, if you will (part of the array, [1], [2]) has a different piece of information in it that relates to the class.'
So here is my class;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class CClient
{
public:
string fN, lN; // fN = First Name // lN = Last Name
int cN, cr, ci; // cN = Card Number // cr = Credit //
void set_values(string, string, int, int, int);
string firstName(){return (fN);}
string lastName(){return (lN);}
int cardNumber(){return (cN);}
int clientID() {return (ci);}
int credit(){return (cr);}
};
void CClient::set_values(string fName, string lName, int cNum, int cred, int cID)
{
fN = fName;
lN = lName;
cN = cNum;
cr = cred;
ci = cID;
};
|
I was wandering if i could create an instance of the class,
CClient customer;
. If having a class like this in or part of an array as explained above is not possible. How would I go about storing this into a .txt and being able to read it again afterwards.
So far for inputting the information into a file i have this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
ofstream customerRecords ("customerRecords.txt", ios::app);
if (customerRecords.is_open())
{
customerRecords << "---------------------------------------------"<<endl;
customerRecords << "CUSTOMER RECORDS: "<<recordAmount<<endl;
customerRecords << "---------------------------------------------"<<endl;
customerRecords << "Customer ID: "<<ID<<endl;
customerRecords << "Customer Name: "<<customer.fN<<" "<<customer.lN<<endl;
customerRecords << "Customer Card Number: "<<customer.cN<<endl;
customerRecords << "---------------------------------------------"<<endl;
customerRecords.close();
cout<<" ...Record Successfully Entered..."<<endl<<endl<<endl;
} else cout<< " ***Unable To Open File***"<<endl<<endl;
|
With this, and with the little knowledge i have i very much doubt that this will be able to be read afterwards, as in if i re-open the program or run the .exe again and use a
/search feature that i implement.
HERE IS THE COMPLETE CODE I HAVE SO FAR, I HAVE SURPASSED THE ASSIGNEMT REQUIREMENT AND I SEEK THIS KNOWLEDGE JUST TO EXPAND MY OWN SKILLS... So no rush and post whatever may help if you want to...
P.S. Sorry if the code is a mess, its not complete and still in parts, thanks in advance for any help i may recieve :)
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
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------------------------
// Variables
//----------------------------------------------------------------------------------------------------------------
string choice;
bool run = true;
int ID=0;
//----------------------------------------------------------------------------------------------------------------
// Classes
//----------------------------------------------------------------------------------------------------------------
class CClient
{
public:
string fN, lN; // fN = First Name // lN = Last Name
int cN, cr, ci; // cN = Card Number // cr = Credit //
void set_values(string, string, int, int, int);
string firstName(){return (fN);}
string lastName(){return (lN);}
int cardNumber(){return (cN);}
int clientID() {return (ci);}
int credit(){return (cr);}
};
void CClient::set_values(string fName, string lName, int cNum, int cred, int cID)
{
fN = fName;
lN = lName;
cN = cNum;
cr = cred;
ci = cID;
};
//----------------------------------------------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------------------------------------------
void newCustomer () // Function To Add A New Customer
{
if (choice == "/new" || choice == "/NEW")
{
int recordAmount=0;
CClient customer;
customer.ci = ID;
ID++;
cout<<" ...Client ID... : "<<ID<<endl<<endl;
cout<<" Please Enter The Customers First Name: ";
cin >>customer.fN; cout<<endl;
cout<<" Please Enter The Customers Last Name: ";
cin >>customer.lN; cout<<endl;
cout<<" Please Enter The Customers Card Number: ";
cin >>customer.cN; cout<<endl<<endl;
ofstream customerRecords ("customerRecords.txt", ios::app);
if (customerRecords.is_open())
{
customerRecords << "---------------------------------------------"<<endl;
customerRecords << "CUSTOMER RECORDS: "<<recordAmount<<endl;
customerRecords << "---------------------------------------------"<<endl;
customerRecords << "Customer ID: "<<ID<<endl;
customerRecords << "Customer Name: "<<customer.fN<<" "<<customer.lN<<endl;
customerRecords << "Customer Card Number: "<<customer.cN<<endl;
customerRecords << "---------------------------------------------"<<endl;
customerRecords.close();
cout<<" ...Record Successfully Entered..."<<endl<<endl<<endl;
} else cout<< " ***Unable To Open File***"<<endl<<endl;
}
}
void searchCustomer () // Funtion To Search For Customers
{
if (choice == "/search" || choice == "/SEARCH")
{
cout<<" ...FEATURE NOT YET IMPLIMENTED..."<<endl<<endl;
}
}
void takeCredit () // Funtion To Take Credit From The Customers Card
{
}
void addCredit () // Funtion To Add Credit To The Customers Card
{
}
void exit () // Function To Exit The Program
{
if (choice == "/exit" || choice == "/EXIT")
{
run = false;
}
}
//----------------------------------------------------------------------------------------------------------------
// MAIN
//----------------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
while (run == true)
{
run = true;
cout<<endl;
cout<<"To Enter A New Record Type: /new"<<endl;
cout<<"To Search For A Record Type /search"<<endl;
cout<<"To Exit The Program Type /exit"<<endl<<endl;
cin>>choice; cout<<endl;
newCustomer();
searchCustomer();
exit ();
}
return EXIT_SUCCESS;
}
|