Wednesday 15 February 2012

Container for double and a pointer C++? -



Container for double and a pointer C++? -

i have 2 questions. 1. using effective container holding [double, object*]? 2. if map selection should use, how in world access object beingness pointed 1 time it's in map? trying print map in function printmap() without luck. tried multiple variations.

void printmap() { //how verify what's in map??!!!!!!!!!!!!!!!!!!!!!!!! (it = cdatabase.begin(); != cdatabase.end(); it++) { double temp = 0; temp = it->first; cout << "key: " << it->first << endl; cout << cdatabase[it->first]->getfirstname(); } }

thanks in advance

erik

ps: included entire .cpp file.

class="lang-cpp prettyprint-override">//erik plachta //purpose: creating client relations management system. should able create employees , customers can stored within database. #include<fstream> //for client database #include<map> //for client database #include<sstream> //for client database #include<stdlib.h> //for client database #include <iostream> using namespace std; /////////////////////////////////////////////////////////////////////////////// /////employee///////////employee////////////employee/////////employee////////// /////////////////////////////////////////////////////////////////////////////// class employee { string firstname, lastname, password, username; //used create unique sessions each user. login info bool manager; //if manager, have spec privlages within app. int sessiontime; //used record login/logoff time. public: employee(string fn, string ln, string pass, string usern) { //first name, lastly name , pass setname(fn, ln); setpassword(pass); setusername(usern); } string getname() { string fullname = firstname + " " + lastname; homecoming fullname; } bool confirmpassword(string pass) { //used test if true or false without giving programme password back. bool status = false; if (pass == password) { status = true; } homecoming status; } bool confirmusername(string usern) { bool status = false; if (usern == username) { status = true; } homecoming status; } private: void setname(string fn, string ln) { firstname = fn; lastname = ln;} void setpassword (string pass) { password = pass; } void setusername(string usern) { username = usern; } }; void printemployee(employee employee) { //this confirming password entered same in database. there private database holding user login info bool status; cout <<"name: " << employee.getname() << endl; status = employee.confirmpassword("123456"); cout << "password test: " << status << endl; status = employee.confirmusername("eplachta"); cout << "username test: " << status << endl; } /////////////////////////////////////////////////////////////////////////////// ///customer////////////customer/////////customer/////customer//////customer//// /////////////////////////////////////////////////////////////////////////////// class client { string firstname, lastname, phonenumber, email; double customernumber; public: client (string fn, string ln, string phonen, string em, double customernum) { setname(fn, ln); setphonenumber(phonen); setemail(em); setcustomernumber(customernum); } string getfirstname() { homecoming firstname; } string getlastname() { homecoming lastname; } string getphonenumber() { homecoming phonenumber; } string getemailaddress(){ homecoming email; } double getcustomernumber() { homecoming customernumber; } void printcustomer() { cout << "first name: " << getfirstname() << endl; cout << "lastname: " << getlastname() << endl; cout << "email: " << getemailaddress() << endl; cout << "phonenumber: " << getphonenumber() << endl; cout <<"customernumber: "<< getcustomernumber() << endl; } private: void setname(string fn, string ln) { firstname = fn; lastname = ln; } void setphonenumber(string phonen) { phonenumber = phonen; } void setemail(string em) { email = em; } void setcustomernumber(double customernum) { customernumber = customernum; } } ; //print client uptop here now. set it's own class /////////////////////////////////////////////////////////////////////////////// ///customer database///////////////customer database///////customer database/// /////////////////////////////////////////////////////////////////////////////// class customerdatabase { double customernumbers; // used increment 100000 - 99999. easy way give out new numbers , prevent duplicates. map <double, customer*> cdatabase; map <double, customer*>:: iterator it; public: customerdatabase() { customernumbers = 100000; loadcustomers(); } void printmap() { //how verify what's in map??!!!!!!!!!!!!!!!!!!!!!!!! (it = cdatabase.begin(); != cdatabase.end(); it++) { double temp = 0; temp = it->first; cout << "key: " << it->first << endl; cout << cdatabase[it->first]->getfirstname(); } } double newcustomernumber() { // used generate unique client ids easily. double customernumberholder = customernumbers; customernumbers++; homecoming customernumberholder; } bool savecustomer(double customern, client *c) { //saves client database 1 time created **not working yet addcustomer(customern, c); ofstream databasefile; //filestream local output bool savecustomer = false; if (checkforcustomernumber(customern) != false) { // if client isn't in database ostringstream stream; // used create int string. local stream maintain clear stream << c->getcustomernumber(); databasefile.open ("database/databasefile.csv", ios::app); string customerholder = c->getfirstname() + "," + c->getlastname() + "," + c->getphonenumber() + "," + c->getemailaddress() + "," + stream.str() + "\n" ; databasefile << customerholder; databasefile.close(); savecustomer = true; } cout << "customer saved status: " << savecustomer << endl; homecoming savecustomer; } bool addcustomer(double customern, client *c) { bool addcustomer = false; cdatabase[customern] = c; homecoming addcustomer; } private: bool loadcustomers() { string firstn, lastn, phonen, emaila; double customern; const int firstname = 0; const int lastname = 1; const int phonenumber = 2; const int email = 3; const int customernumber = 4; //skipped 5 because every new line runs through while loop. const int addtodatabase = 5; ifstream databasefile; //file-stream local input databasefile.open ("database/databasefile.csv", ios::in); string line = ""; int = 0; double loadedcustomers = 0; //used maintain track of client loaded. while( getline(databasefile, line, ',')) { if (i == firstname) { firstn = line; i++; } else if (i == lastname) { lastn = line; i++; } else if (i == phonenumber) { phonen = line; i++; } else if (i == email) { emaila = line; i++; } else if (i == customernumber) { double customernumb = atoi(line.c_str()); customern = customernumb; client c(firstn, lastn, phonen, emaila, customern); client *holder; holder = &c; addcustomer(c.getcustomernumber(), holder); = 0; //resetting next line. } //this how saving client database. dont' need save when loading customers. saving code incase needed// savecustomer(c.getcustomernumber(), c); } databasefile.close(); } bool checkforcustomernumber(double customern) { //if id # within database, homecoming true id exists bool customernumbermatch = false; if((cdatabase.find(customern)) != (cdatabase.end())) { //if client number key map, not reach end. therein client in database customernumbermatch = true; customernumbers++; } cout << "c. number match satus: " << customernumbermatch << endl; homecoming customernumbermatch; } }; /////////////////////////////////////////////////////////////////////////////// //main////////////////main/////////////main////////////////main/////////main/// /////////////////////////////////////////////////////////////////////////////// int main() { //customer database creation customerdatabase customerdatabase; //creating database ////customer client jt("johnny", "tester", "989-123-4567", "johnnytester@gmail.com", customerdatabase.newcustomernumber()); // used create test client client test ("this", "is", "a", "test", 100000); client tt("timmy", "tester", "989-989-9898", "erik@itsatest.com", customerdatabase.newcustomernumber()); //customer database used client *holder; holder = &jt; customerdatabase.savecustomer(holder->getcustomernumber(), holder); holder = &tt; customerdatabase.savecustomer(holder->getcustomernumber(), holder); holder = &test; customerdatabase.savecustomer(holder->getcustomernumber(), holder); customerdatabase.printmap(); //employee employee ep ("erik", "plachta", "123456", "eplachta"); //creating test employee // printemployee(ep); //making sure employee can login / created correctly. cin.get(); homecoming 0; } //customer class looks @ database id wants use,

before starting on selection of containers, touch upon selection of datatype id numbers. using double, floating point number, require integer values, much more sensible utilize integer type.

the selection of container depends on how plan access , modify info in container. key characteristics consider are:

do need able access elements in container in random sequence? ie. elements id? containers map, unordered_map, vector. lists bad this, because have traverse list find element want.

do need able iterate through values in predetermined order (eg. key order)? containers map, vector, various list types.

do need able insert elements @ given position? or plenty insert @ end? if former map, unordered_map, list good. if latter vector good.

what type of key going used values? can entries in vector integer number, if have, example, string, need utilize map or unordered_map.

if key numeric - list sparse? ie. there big gaps in sequence? if yes vector less attractive because there may big amounts of wasted memory.

the characteristics seem be:

you need able customers id you need able iterate through list print out database. not sure if requirement in order, let's assume is. it looks though adding new entries @ end of list. the keys integer numbers i not sure if list sparse. if delete lot of customers middle there may gaps.

if not lastly point, vector might improve selection map. vectors inexpensive , have high performance of operations use.

anyway, if decide map best, based on know of application, when iterate through map, iterator points towards value_type of container, in case of map of type std::pair, k key type , v value type. value type stores key , value, access using 'first' , 'second' members.

for (it = cdatabase.begin(); != cdatabase.end(); it++) { cout << "key: " << it->first << endl; cout << it->second->getfirstname() << endl; }

c++ pointers containers

No comments:

Post a Comment