void winerys::info(winerys & wine)
	{
		
		ofstream outfile("page.txt");

		node *temp;
		
		temp  = new node;

		
		cout << "please enter date the winery went into business" << endl;
		cin  >> temp->start;
		cout << "please enter the loaction of winery" << endl;
		cin  >> temp->name;
	    cout << "plese enter the name of winery occupies" << endl;
		cin  >> temp->area;
		cout << "please enter the overall success of winery, good or bad," << endl;
        cin  >> temp->rating;
		cout << "please enter the num  of acres the winery" << endl;
		cin  >> temp->num;
		temp->nxt = NULL;
	
		
		outfile << temp->start <<';' <<temp->name <<';'<< temp->area 
		<<';'	<< temp->rating<<';' << temp->num <<';'<< endl;

	
		outfile.close();

	}




list::list(char * fileName)
	{
    
	
	ifstream infile("page.txt");
	
	headByName = new lnode;
	headByName->item = curr->item;

	wine =  winerys( curr->str,curr->nam,curr->are,curr->rat,curr->nu); 
	
	infile.close();

	insert(wine);
	
	}

i cant seem to load a linked list "double threaded" in the list class , from the winery class. and then pass "wine" -Which hold all of the info about the winery::::

Recommended Answers

All 7 Replies

winerys::info() contains a huge memory leak -- you allocate temp with new operator but never delete it before the function ends.

why is that constructor opening a file for reading then never reading anything ????

winerys::info() contains a huge memory leak -- you allocate temp with new operator but never delete it before the function ends.

why is that constructor opening a file for reading then never reading anything ????

why? because i do not have the code to read it in. my main contains a crtdbg declartion; so it prevents memory leak. i need to open the txt file containg info, then have the data in the file put into a linked list - which is double threaded-

node *temp is declared local to the info function. You allocate memory with the new operator, and have some cin lines to enter information from the keyboard. Then the function ends without doing anything with temp, it just tosses all that into the bit bucket leaving the allocated memory somewhere that can no longer be accessed.

>>my main contains a crtdbg declartion; so it prevents memory leak.
No it doesn't. crtdbg does no such thing. Read this

>> i need to open the txt file containg info, then have the data in the file put into a linked list - which is double threaded
What ok, but your program doesn't do that.

node *temp is declared local to the info function. You allocate memory with the new operator, and have some cin lines to enter information from the keyboard. Then the function ends without doing anything with temp, it just tosses all that into the bit bucket leaving the allocated memory somewhere that can no longer be accessed.

>>my main contains a crtdbg declartion; so it prevents memory leak.
No it doesn't. crtdbg does no such thing. Read this

>> i need to open the txt file containg info, then have the data in the file put into a linked list - which is double threaded
What ok, but your program doesn't do that.

you were right, i added delete"pointer" to all functions that i created new "". now i think i am free of memory leak---- now how do \i read in the file, each individual data type needs to be stored in the new node;;i

read it in very similar to the way you wrote it out

node *temp;
ifstream infile("page.txt");
temp  = new node;
while( infile >> temp->start) // read until end-of-file
{
    infile >> temp->name >> temp->area // etc. etc
    // add temp to the linked list.  You didn't post that part so I can't help you with that
}
#ifndef List_h
	#define List_h
	#include "winerys.h"
	
    
	class  list 
	{
	public:
		~list();
		list();
	    list(char * filename);
		list(const list *alist); 
		const list& operator = ( const list &aList);
		bool insert(const winerys& item);
		bool remove(char * key);
		void displaybyrating();
		void displaybyname();
		bool searchwinery(char * key, winerys& wine)const;
		void writeOut(char * fileName)const;
		
	private: 
	
		struct lnode
	 { 
	    winerys item;
	    lnode * nextbyname;
	    lnode *nextbyrating;
	     
	 };
		lnode * headByName;
		lnode * headByRating;
		int size;
	};
	
	#endif
#include <stdlib.h>
	#include <crtdbg.h>
	#include "winerys.h"
	#include "List.h"
	#include <iostream>
	
	using namespace std;

	void displayMenu(); 
	char getCommand();
	void executeCmd(char command);

	int main () 
	{

	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 
    
	
	char command = 'a';
	char fileName[] = "page.txt";
	list winerys(fileName);

	displayMenu();
	command = getCommand();
	while(command != 'q')
	{
		
	executeCmd(command );
	displayMenu();
	command = getCommand();
	}
	winerys.writeOut(fileName);
	cout << "\nThank you for using CWMS!" << endl << endl;
	return 0;

	}

	void displayMenu()
	{
	cout << "\nImplemented by:josaih kolar-geren,cs260" << endl;
	cout << "\nWelcome to my winery data base! "<< endl;
	cout << "\ta> add a winery" << endl;
	cout << "\tr> remove a winery" << endl;
	cout << "\ts> search for a winery" << endl;
	cout << "\tl> display by rating" << endl ;
	cout << "\tk> display by name"   << endl;
	cout << "\ts  save a winery"     << endl;
	cout << "\tq> quit the application" << endl << endl;
	}

	char getCommand()
	{
	char cmd;
	cout << "Please enter your choice (a, r, s, l , k or q):";
	cin >> cmd;
	cin.ignore(100, '\n');
	return tolower(cmd);
	}

	void executeCmd(char command)
	{  
   
	winerys b;
	winerys wine;
	list alist;
	char key[23];

	switch(command)
	{
	case 'a':   b.info();
			    alist.insert(wine);
		break;
	case 'r':	cout << "\nPlease enter the name of the winery you want to remove: " << endl;
				alist.remove( key);
			  
		break;
	case 'f':	cout << "\nPlease enter the name of the winery you want to search: " << endl;  
				alist.searchwinery(key, wine);
				
		break;
	case 'l':	cout << "display by rating" << endl;
				alist.displaybyrating();
		break;
	case 'k':	cout << "display by name" << endl;
				alist.displaybyname();
					
		break;
	case 's':	alist.writeOut( "lst.txt");
		 		cout << "the winery has been saved in the database. " << endl;
		break;
	default:	 cout << "illegal command!" << endl;
		break;
	}
}
#ifndef Winerys_h
	#define Winerys_h
	#include <iostream>

	using namespace std;

	
		class winerys
	{
	
	public:
	
	friend ostream& operator << (ostream& out, const winerys& awine);
	
	~winerys();	
	winerys();
	const winerys(const winerys& awinery);
	void info();
	const winerys& operator=(const winerys& wine);
	
	
	private:
	
	float start;
	char *name;
    char * area;
	float  num;
	float rating; 
struct node 
	{
	
	int  start;
	char  name[10];
	
    char area[20];
	float num;
	float rating; 
	node * head;
	node * nxt;
	node * item;
#include "List.h"
	#include <iostream>
	#include <iomanip>
	#include <fstream>
	#include <cassert>

	using namespace std;

	
	list::~list()
	{
		
		
	}

		
	list::list() : headByName(NULL),headByRating(NULL)
	{

	}
	
	list::list(const list *alist)
		: size(alist->size)  
	{


	if(alist->headByName == NULL)
		headByName = NULL;
	
	else 
	{
		headByName = new lnode;
		headByName->item = alist->headByName->item;
		lnode *newptr = headByName;
	
	for(lnode * orgptr = alist->headByName->nextbyname;
			orgptr!=NULL;
			orgptr = orgptr->nextbyname)

		{newptr->nextbyname = new lnode;
		 newptr = newptr->nextbyname;
	     newptr->item = orgptr->item;

		}
		
		 newptr->nextbyname =NULL;
	}
	}

	list::list(char * filename)
	{

	ifstream infile("page.txt");	
	
	int nextitem;
	
    if(infile >> nextitem)
	{

	headByName = new lnode;

	headByName->item = nextitem;
	headByName->nextbyname = NULL;

	infile.close();
	}
	
	}

		
		
		
		const list& list::operator =( const list &alist)
		{
			{
		if(this == &alist)
			return *this;
		else
		{
		
		lnode * curr = headByName;
		while(headByName)
		{
			curr = headByName->nextbyname;
			delete headByName;
			headByName = curr;
		}

		
		if(!alist.headByName)
			headByName = NULL;
		else
		{
			
			headByName = new lnode;
			assert(headByName != NULL);
			headByName->item = alist.headByName->item;

			
			lnode * destNode = headByName;
			lnode * srcNode = alist.headByName->nextbyname;

			while(srcNode)
			{
				destNode->nextbyname = new lnode;
				assert(destNode->nextbyname);
				destNode = destNode->nextbyname;
				destNode->item = srcNode->item;

				srcNode = srcNode->nextbyname;
			}
				destNode->nextbyname= NULL;
		}

			return *this;
		}
		}
		}
		bool list::insert(const winerys &awine)
	{

	lnode * prev = NULL;
	lnode * curr = headByName; 
	while (curr!=NULL && curr->item < awine)

	{
		prev = curr;
		curr = curr->nextbyname;
	}

	if(curr && curr->item == awine )
		return false;
	
	else
	{
		
		lnode * newNode = new lnode;
		newNode->item = awine;
		newNode->nextbyname = NULL;

		newNode->nextbyname = curr;
		if(prev == NULL)
			headByName = newNode;
		else
			prev->nextbyname = newNode;
	
		return true;
	}
	}


	bool list::remove (char * key)
	{
	char name[100];

	
	lnode * prev = NULL;
	lnode * curr = headByName;
	while (curr)
	{
	

		if(strcmp(key, name) == 0)
		{
			
			if(!prev)
				headByName = curr->nextbyname;
			else
				prev->nextbyname = curr->nextbyname;
			delete curr;
			size--;
			return true;
		}

		prev = curr;
		curr = curr->nextbyname;
	}

	return false;
	}


	void list::writeOut(char * fileName)const
	{
	ofstream out;
	winerys awine;
	out.open("lst.txt");
	if(!out)
	{
		cerr << "fail to open " << fileName << " for output!" << endl;
		exit(1);
	}

	lnode * curr;

	float  start;
	char name[100];
    char area[10];
	float num;
	float rating; 
    
	
       
	
	
	

	out.close ();
	}


bool list::searchwinery(char * key, winerys & wine)const
	{
	
	lnode * curr;
	char name[10];
	for(curr=headByName; curr; curr=curr->nextbyname)
	{
		
		if(strcmp(key, name) == 0)
		{
			wine = curr->item;
			return true;
		}
	}

	return false;
	}

	void list::displaybyname()
	{
	
	lnode * curr;

	cout << "Data in the list: " << endl << endl;
	
	for(curr=headByName; curr; curr=curr->nextbyname)
	{
		cout << '\t' << curr->item << endl;
	}
}


	void list::displaybyrating()
{
	lnode * curr;

	cout << "Data in the list: " << endl << endl;
	for(curr=headByRating; curr; curr=curr->nextbyrating)
	{
		
		cout << '\t' << curr->item << endl;
	}
}
#include "Winerys.h"
	#include <iostream>
	#include <iomanip>
	#include <cassert>	
	#include <fstream>
	using namespace std;

	winerys::~winerys()
	{
	node * curr = head;
	while(head)
	{
	curr = head->nxt;
	delete head;		
	head = curr;
	} 
	}
	winerys::winerys():head(NULL),nxt(NULL),size(0)
	{
		
		
	}
	
	
	winerys::winerys(const winerys &awinery)
		: size(awinery.size)
	{


	if(awinery.head == NULL)
		head = NULL;
	
	else 
	{
	head = new node;
	head->item = awinery.head->item;
	node *newptr = head;
	
	for(node * orgptr = awinery.head->nxt;
			orgptr!=NULL;
			orgptr = orgptr->nxt)
		{ newptr->nxt = new node;
		  newptr = newptr->nxt;
		  newptr->item = orgptr->item;

		}
		
		newptr->nxt =NULL;
	}
	}
	
	
	const winerys& winerys::operator=(const winerys &wine)
	{
	
	if(this == &wine)
		return *this;
	
	else
	{   
		
		return *this;
	}
	}

	bool operator < (const winerys& d1, const winerys& d2)
	{
	char name1[100];
	char name2[100];

	

	if(strcmp(name1, name2) < 0)
		return true;
	else
		return false;
	}

	bool operator ==(const winerys& d1, const winerys& d2)
	{

		
		
	char name1[100];
	char name2[100];

	

	if(strcmp(name1, name2) == 0)
		return true;
	else
		return false;




		
		/*{
	if(this == &awine)
		return *this;
	else
	{
		
		node * curr = head;
		while(head)
		{
			curr = head->nxt;
			delete head;
			head = curr;
		}

		
		if(!awine.head)
			head = NULL;
		else
		{
			
			head = new node;
			assert(head != NULL);
			head->item = awine.head->item;

			
			node * destNode = head;
			node * srcNode = awine.head->nxt;

			while(srcNode)
			{
				destNode->nxt = new node;
				assert(destNode->nxt);
				destNode = destNode->nxt;
				destNode->item = srcNode->item;

				srcNode = srcNode->nxt;
			}
			destNode->nxt = NULL;
		}

		return *this;
	}
}*/

}

	void winerys::info()
	{
		ofstream outfile("page.txt");
		node * str_prt =NULL;
		node *temp ;
		node * curr;
		
		temp  = new node;

		
		cout << "please enter date the winery went into business" << endl;
		cin  >> temp->start;
		cout << "please enter the loaction of winery" << endl;
		cin  >> temp->area;
	    cout << "plese enter the number of acres the winery occupies" << endl;
		cin  >> temp->num;
		cout << "please enter the overall success of winery, good or bad," << endl;
        cin  >> temp->rating;
		cout << "please enter the name of the winery" << endl;
		cin  >> temp->name;
		temp->nxt = NULL;

		

		outfile << temp->start<<';' << temp->area <<';' <<temp->num <<';' << temp->rating 
			<< ';'<< temp->name <<';' << endl;
		
		
		outfile.close();

		}


	
		
		

     
	
	ostream& operator << (ostream& out, const winerys &awine)
		{ 
	
	
	out <<  setw(20) << awine.name << setw(20) << awine.area << setw(15) << awine.start << setw(15) 
		<< awine.num << setw(15)<< awine.rating << setw(15) <<endl;
		return out;
		}

this is all the code 2 weeks worth need help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Have you compiled and tested any of the code you posted ? Before you go any further you need to actually test all that code to see if it works correctly. Compile without error != work correctly.

There seems to be a lot of logic errors in your code, for example line 125 of List.cpp while (curr!=NULL && curr->item < awine) That won't work. why? because curr->item is a pointer and awine is a reference to an object. Not the same things so I doubt that code will even compile.

You are just wasting your efforts by attempting to add more to that code when the code you have won't compile and has not been tested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.