Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 27 Nov 2014, 03:07
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
C++ database coding attempt
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 2 of 2 Posts_count   Goto page: Previous 1, 2
Author Message
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Mon 12 Oct 2009, 06:02    Post_subject:  

Quote:
As I recall, it had bugs.


OK - will bear that in mind. It is a shame it is buggy because generating the GUI was very straightforward.

_________________
Puppy WIKI
Back to top
View user's profile Send_private_message Visit_website 
tronkel


Joined: 30 Sep 2005
Posts: 1104
Location: Vienna Austria

PostPosted: Thu 15 Oct 2009, 07:13    Post_subject:  

@ed

Here is the latest C++ code for the contacts database
Add, delete, search and list all contacts have been implemented

It uses an STL list<Contacts> as the main data structure
You could play around and have a go with a vector or map. Trouble with vectors are that it is difficult to access them in the middle if you need to insert or delete something. It can be done but it's a lot of bother and very error-prone. Much the same as with a C-style array.

The map though is a better idea. It uses a red-black binary tree as its base container and is therefore lightning fast on search and insert. You can also quite easliy make your own binary tree, but there is no specific STL version i.e. you would need to write it yourself.

Instead of using in.read() and out.write() this code implements an overloaded operator for << and >> that allows input and output of entire objects direct to and from disk file.

Here's the code:

contact.h

Quote:
#ifndef CONTACT_H
#define CONTACT_H

#include <string>
#include <iostream>
#include <fstream>
#include <list>
using namespace std;


class Contact
{//class definition for Contact object

friend ofstream & operator<<(ofstream &, Contact &);
friend ifstream & operator>>(ifstream &, Contact &);

private:

string name;
string address;
string email;

public:

Contact(); //default constructor
Contact(string, string, string); //parameterised constructor
string returnName();
string returnAddress();
string returnEmail();
void setName(string);
void setAddress(string);
void setEmail(string);








};

#endif



main.cpp


Quote:
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include "contact.h"
#include <stdlib.h>
#include <cctype>


using namespace std;

//globals
//forward declarations of menu functions

void quit(list<Contact> alist);
Contact makeContact(list<Contact> & alist);
void search(list<Contact> alist);
Contact displayMenu();
void initdata();
void kill();
void listall(list<Contact> alist);

list<Contact> mainlist; // this structure holds the main list of contacts


int main()
{


initdata(); //load data from file and display user menu


}


void quit(list<Contact> alist)
{// quit and save changes to disk
ofstream output;
output.open("lob");

list<Contact>::iterator li=alist.begin();
for(;li!=alist.end();li++)
{
output << *li;

}

output.close();

}

Contact makeContact(list<Contact> & alist)
{// ask user for details of new contact
string name,address,email;

cout << "Please enter a name" << endl;
getline(cin,name);
cout << "Please enter an address" << endl;
getline(cin,address);
cout << "Please enter an email address" << endl;
getline(cin,email);
Contact newcontact(name,address,email);
alist.push_back(newcontact); //insert into mainlist
quit(mainlist);//save back to disk
return newcontact;
}

void search(list<Contact> alist)
{//search mainlist based on name

system("clear");
string term="";
cout << "please enter name" << endl;
getline(cin,term);
list<Contact>::iterator ali;
ali=alist.begin();
for(;ali!=alist.end();ali++)
{
if(ali->returnName()==term)
{
cout << "Your search has returned the following record(s) >> " << endl << endl;
cout << "name: " << ali->returnName() << endl;
cout << "address: " << ali->returnAddress() << endl;
cout << "email: " << ali->returnEmail() << endl << endl;

}

}

getchar();
}

Contact displayMenu()
{

int choice;
Contact acontact;

do{//this loop redraws menu unless quit is selected

cin.clear();
system("clear");
cout << "Welcome to Lobster's Database!" << endl << endl;
cout << "YOU HAVE " << mainlist.size() -1 << " CONTACTS IN YOUR DATABASE" << endl << endl;
cout << "enter your choice >>" << endl;

cout << "1. Add contact" << endl;
cout << "2. List all contacts" << endl;
cout << "3. Search for a contact" << endl;
cout << "4. Delete a contact" << endl;
cout << "5. Quit" << endl;


cin >> choice;
cin.ignore();
if(choice < 1 || choice > 5 )
{
cout << "you must select a NUMBER from the menu list" << endl;
exit(1);
}




switch(choice)
{
case 1: makeContact(mainlist);
break;
case 2: listall(mainlist);
break;
case 3: search(mainlist);
break;
case 4: kill();
break;
case 5: quit(mainlist);
break;
}

}while(choice!=5); //end of do loop

return acontact;



}

void initdata()
{
ifstream input;
input.open("lob");


while(input.good() )
{//recover saved data from disk file and insert into mainlist
Contact acontact;
input >> acontact;

mainlist.push_back(acontact);
}

input.close();


displayMenu(); //show main menu

}

void kill()
{
string temp="";
char ans='z';
list<Contact>::iterator xi;
cout << "This action will permanently delete a selected contact from your database!" << endl;
cout << "name of contact to delete >> " << endl;
getline(cin,temp);
cout << "Are you sure? enter y or n" << endl;
ans=getchar();
cin.ignore();
if(ans=='Y'||ans=='y')
{
xi=mainlist.begin();
for(;xi!=mainlist.end();xi++)
if(xi->returnName()==temp)
{
mainlist.erase(xi);
cout << "contact " << temp << " has been deleted from your list of contacts" << endl << endl;
cout << "Press enter to exit" << endl;
getchar();
quit(mainlist);
return;
}
}

else
cout << "deletion of contact " << temp << " aborted" << endl << endl;
cout << "Press enter to exit" << endl;
getchar();


}

void listall(list<Contact> alist)
{
cout <<"Your list of all contacts: " << endl <<endl;
list<Contact>::iterator li=alist.begin();
for(;li!=alist.end();li++)
{

cout << li->returnName() << endl;
cout << li->returnAddress() << endl;
cout << li->returnEmail() << endl << endl;

}
cout << "Press enter to exit. Have a nice day Cool" << endl;
getchar();


}


Quote:
contact.cpp

//#include "toplevel.h"
#include <string>
#include <fstream>
#include <list>
#include "contact.h"

using namespace std;


Contact::Contact()
{ } //default ctor

Contact::Contact(string aname, string anaddress, string anemail)
{// definition of Contact object constructor

name=aname;
address=anaddress;
email=anemail;

}

string Contact::returnName()
{
return name;

}


string Contact::returnAddress()
{
return address;

}


string Contact::returnEmail()
{
return email;

}

void Contact:: setName(string name)
{
this->name=name;
}

void Contact:: setAddress(string address)
{
this->address=address;
}

void Contact:: setEmail(string email)
{
this->email=email;
}

ofstream & operator<<(ofstream & output, Contact & acontact)
{//def of overloaded << operator


output << acontact.returnName() << "\n"
<< acontact.returnAddress() << "\n"
<< acontact.returnEmail() << "\n";
}

ifstream & operator>>(ifstream & input, Contact & acontact)
{
string name,address,email;
input >> name >> address >> email ;

acontact.setName(name);
acontact.setAddress(address);
acontact.setEmail(email);

}



_________________
Life is too short to spend it in front of a computer
Back to top
View user's profile Send_private_message Visit_website AIM 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Thu 15 Oct 2009, 08:47    Post_subject:  

Quote:
Here is the latest C++ code for the contacts database


will have a look soon
must go into the real world
(just to make sure it is still there . . .)
be right back . . .

_________________
Puppy WIKI
Back to top
View user's profile Send_private_message Visit_website 
tronkel


Joined: 30 Sep 2005
Posts: 1104
Location: Vienna Austria

PostPosted: Thu 15 Oct 2009, 09:33    Post_subject:  

Lobster wrote:

Quote:
must go into the real world
(just to make sure it is still there . . .)


The real world ?????
What is it!

If you find anything worth knowing about, please let me know! I went there once, but that was so long ago I've forgotten what it was like.

You mean there are actually OTHER people out there????

I've also been informed though, that there are people out there who don't just sit at their PC's all day. What do they do all day I wonder?

Be careful out there. They say it can be fun but VERY dangerous! Would like to hear of any interesting real-world activities (if such things exist).

_________________
Life is too short to spend it in front of a computer
Back to top
View user's profile Send_private_message Visit_website AIM 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Thu 15 Oct 2009, 10:42    Post_subject:  


Where I went mostly trees and sky and such like . . . stuff
Nice day . . . and now back to the virtual world
OK got the program working now playing Smile
Smile

_________________
Puppy WIKI
Back to top
View user's profile Send_private_message Visit_website 
Display_posts:   Sort by:   
Page 2 of 2 Posts_count   Goto page: Previous 1, 2
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0703s ][ Queries: 11 (0.0032s) ][ GZIP on ]