![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
linked list database
I am having a couple problems with a database I created.
1)I cant add anymore than 2 records to it. 2)It doesnt display all the records in the database I dont have the source code right off, but will post it soon. I was just wondering if anyone could help. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
We're kinda gonna need source code to figure this out, mate.
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Quote:
If you know how to use (or have access to) DDD, that's a great debugger to see how the linked list is built. ps: seems like you are trying to insert nodes in alphabetical order, that may be the trouble spot too. |
|
|
|
|
|
|
#4 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
here's the source...its kinda long so im adding it as an attachment.
|
|
|
|
|
|
#5 |
|
Programmer
|
i cant test the source since I'am not able to use winsock... I'll look through the code soon, because i had a similar prob in my C-times..... well, there are some things i'm interested in ... whats the reason U are using more than 7 year old code ( meaning the headers) ... why arend you usind std::string instread of char* .. this would make u able to use operator == / operator< instead of strcmp.... and why do u write a own linked list in c++ ? well if it is just training , then there is nothing wrong about that , but deriving from std::list would be more comfortable , wouldn't it ?
__________________
of all the things he has lost, i think he misses his mind the most typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza; |
|
|
|
|
|
#6 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
i like using old code...its easy on my mind...lol
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
There is no excuse for not writing code that tries to be as standard compliant as posible. Your obveriously a beginner but that fact that you seem to be against writing better code is a bad sign for the future. If you are being taught to use <iostream.h> etc then complain that you want a better teacher
.I rewrote your position_insertion_point function and insertion seems solid for now, although you did get a few null pointer exceptions in other areas (such as trying to display an empty list). record_node *position_insertion_point(char lastname[50])
{
record_node *temp_ptr = 0;
if( head_ptr->next == 0 ) return head_ptr;
if( strcmp(lastname,head_ptr->next->last_name) < 0 ) return head_ptr; // insert after head
temp_ptr = head_ptr->next;
while( temp_ptr->next != 0 )
{
if( strcmp(lastname,temp_ptr->next->last_name) < 0 ) return temp_ptr; // insert after temp
temp_ptr=temp_ptr->next;
}
return temp_ptr; // insert at end
} |
|
|
|
|
|
#8 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
its not that im against learning new code...its just that i learned a certain way and its hard for me to change. If someone could show me how that wouls be great. How much would my program change if i used the code shapless is talking about
|
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
Heh, I see you copied my sig. "An actual quote, that." was commenting on the CNN thing though :-)
Anyway, std::string is a class that facilitates easy handling of strings. I'm not going to explain everything about it right now, but you can look it up in your favorite C++ reference manual OR on the internet.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#10 | |
|
Programmer
|
Quote:
#ifndef _DATABASE_HPP_
#define _DATABASE_HPP_
#include<list>
#include<string>
#include<iostream>
#include<algorithm>
#include<iterator>
//#include<fstream>
namespace database_io{
// example data class
class userdata{
friend const bool operator<(const userdata&,const userdata&);
friend const bool operator==(const userdata &,const userdata &);
friend std::ostream& operator<<(std::ostream &,const userdata &);
public:
explicit userdata(const std::string&n ,const std::string &ln,const std::string &mail, const int &a):name_(n),last_name_(ln),email_(mail),age_(a){}
private:
std::string name_;
std::string last_name_;
std::string email_;
short int age_;
};
/*********************
right now i'm too lazy wo write a save load function, since this is another special case..(for the
userdata class only)...
a way to on my mind to do it :
read:
1 define a max size per string and an delimiter ( shouldnt be ' ' ) then use getline in a loop
"while(!your_ifsream.eof()) to read the strings and the age and then use userdata_factory to insert it... and so on...
write
also use the max size and the same delimiter and std::string.c_str() for example than just write....
maybe this isnt the best way but its of course one way to make it
**************/
// this one is like yours because this functions aint important at all
// because it handles just the specific case userdata
const userdata userdata_factory(){
using std::cin;
using std::cout;
std::string n,ln,mail;
int age;
cin.ignore(80,'\n');
cout<<"\nNEW RECORD\n";
cout<<"\n";
cout<<"First Name: ";
cin>>n;
cin.ignore(80,'\n');
cout<<"Last Name: ";
cin>>ln;
cin.ignore(80,'\n');
cout<<"Email Address: ";
cin>>mail;
#ifdef _VALIDATE_EMAIL_H_
if(!check_io::validate_email(mail))
std::cout<<"this is no real email address\n";
// or return with error code example:
// std::pair<userdata,bool> where
// userdate is the new data and the bool flag shows if the data is correct
#endif
cin.ignore(80,'\n');
cout<<"Age: ";
cin>>age;
#ifdef _VALIDATE_INTEGER_H_
if(check_io::isnum(age))
std::cerr<<"wrong age input\n";
// or set the the pair flag wo wrong
#endif
cin.ignore(80,'\n');
return userdata(n,ln,mail,age);
}
const bool operator<(const userdata& one, const userdata& other){
if(one.last_name_==other.last_name_)
return one.last_name_<other.last_name_;
return one.last_name_==other.last_name_;
}
const bool operator==(const userdata &one,const userdata &other){
if(one.last_name_==other.last_name_)
return one.name_==other.name_;
return false;
}
// do the formatting like you want.. it isnt that important ...
std::ostream& operator<<(std::ostream &ostr,const userdata &dat){
ostr<<"Name : "<<dat.name_<<"\n";
ostr<<"Lastname : "<<dat.last_name_<<"\n";
ostr<<"Email : "<<dat.email_<<"\n";
ostr<<"Age : "<<dat.age_<<"\n";
return ostr;
}
// end of the example
//************************************************
// database class
//************************************************
template<class data_t>
class database{
typedef typename std::list<data_t> data_list_t;
public:
explicit database(){}
const bool add(const data_t &dat);
const bool del(const data_t &dat);
void show_all()const;
const data_list_t& get_list()const{return data_list;}
const bool input();
const bool is_in(const data_t &dat);
const bool del_all();
private:
data_list_t data_;
};
template<class data_t>
const bool database<data_t>::add(const data_t& dat){
if(is_in(dat)){
#ifdef DEBUG
std::cout<<"\t\t\t data already in\n";
#endif
return false;
}
data_.insert(data_.begin(),dat);
data_.sort();
return true;
}
template<class data_t>
const bool database<data_t>::is_in(const data_t & dat){
return (std::find(data_.begin(),data_.end(),dat))!=data_.end();
}
template<class data_t>
const bool database<data_t>::del(const data_t &dat){
if(!is_in(dat) && data_.empty()){
#ifdef DEBUG
std::cout<<"\t\t\t no such data !!\n";
#endif
return false;
}
data_.erase(std::find(data_.begin(),data_.end(),dat));
return true;
}
template<class data_t>
void database<data_t>::show_all()const{
if(data_.empty()){
#ifdef DEBUG
std::cout<<"\t\t\tdata_list is empty!!\n";
#endif
return;
}
copy(data_.begin(),data_.end(),std::ostream_iterator<data_t>(std::cout,"\n\n"));
std::cout<<"\n-------------------------------\n";
return;
}
template<class data_t>
const bool database<data_t>::del_all(){
if(data_.empty()){
#ifdef DEBUG
std::cout<<"\t\t\tdata_list is empty!!\n";
#endif
return false;
}
data_.clear();
return true;
}
}// namespace
#endif
----------------------------------------------------------------------
test main
#include "database.hpp"
int main (){
using namespace database_io;
try{
database<userdata> testbase;
userdata test("foo","bar","foobar@blubb.com",21);
testbase.add(test);
testbase.show_all();
testbase.add(userdata_factory());
testbase.show_all();
testbase.del(test);
testbase.show_all();
testbase.add(test);
testbase.show_all();
if(!testbase.add(test))
std::cerr<<"data in\n";// will be the case
if(!testbase.del_all())
std::cerr<<"del error\n";
if(!testbase.del_all())
std::cerr<<"del error\n";// will be the case
testbase.show_all();
}catch(...){
std::cerr<<"exception\n";
return -1;
}
return 0;
}
__________________
of all the things he has lost, i think he misses his mind the most typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza; |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|