C++ database coding attempt

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#16 Post by Lobster »

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 Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
tronkel
Posts: 1116
Joined: Fri 30 Sep 2005, 11:27
Location: Vienna Austria
Contact:

#17 Post by tronkel »

@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
#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

#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 8-)" << endl;
getchar();


}
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

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#18 Post by Lobster »

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 Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
tronkel
Posts: 1116
Joined: Fri 30 Sep 2005, 11:27
Location: Vienna Austria
Contact:

#19 Post by tronkel »

Lobster wrote:
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

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#20 Post by Lobster »

Image
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 :)
:)
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

Post Reply