Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 17th, 2005, 1:16 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
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.
Attached Files
File Type: zip db.zip.zip (30.5 KB, 16 views)
Brent is offline   Reply With Quote
Old Jul 17th, 2005, 1:34 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
We're kinda gonna need source code to figure this out, mate.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 17th, 2005, 2:12 PM   #3
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Quote:
1)I cant add anymore than 2 records to it.
that sounds to me like all you have is a head and a tail.
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.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 17th, 2005, 7:31 PM   #4
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
here's the source...its kinda long so im adding it as an attachment.
Attached Files
File Type: zip source.zip (2.6 KB, 20 views)
Brent is offline   Reply With Quote
Old Jul 17th, 2005, 8:55 PM   #5
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
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;
Shapeless is offline   Reply With Quote
Old Jul 17th, 2005, 9:57 PM   #6
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
i like using old code...its easy on my mind...lol
Brent is offline   Reply With Quote
Old Jul 17th, 2005, 11:35 PM   #7
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
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
}
Animatronic is offline   Reply With Quote
Old Jul 18th, 2005, 2:43 PM   #8
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
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
Brent is offline   Reply With Quote
Old Jul 18th, 2005, 2:50 PM   #9
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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
uman is offline   Reply With Quote
Old Jul 18th, 2005, 2:52 PM   #10
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
Quote:
Originally Posted by Brent
i like using old code...its easy on my mind...lol
well i dont like it too much..and so i just wrote a base-template class to use.. u could just inherit from it and add the winsock( idoesnt have a windows-machine ) things as well as the save and load functions... well i know thats still a lot and my code isnt exceptions safe at all but there are not many places where smth could throw ( i belive so -.-) and its nice to handle ...

#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;
Shapeless is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC