C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 17th, 2005, 12:13 PM
napsak napsak is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 napsak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 33 sec
Reputation Power: 0
Issue implementing algorithm of Lempel Ziv

I have been struggling with this program for many hours. I've also looked through
most all of the posts listed here for c++ relevant to my issue. The problem is with
my array that holds code word not seen yet, it gets replaced everytime, let
me explain.
I have my dictionary of code words setup as a struct with two
variables "code" and "key". I have no problem creating a dictionary
and I can even add to it. What happens is I do this:
STRING = get input character
WHILE there are still input characters
CHARACTER = get input character
IF STRING+CHARACTER is in the string table then
STRING = STRING+character
ELSE
output the code for STRING
add STRING+CHARACTER to the string table
STRING = CHARACTER
END of IF
END of WHILE

Everything works great the first time through the loop. Then what
happens is not good. The buffer I use to hold the STRING+CHARACTER
writes itself into all the following new structs I create that hold
more code words for the dictionary. Unfortunately my code is not very
clear, but I seem to know it inside and out. Could anyone please help direct me to what I can do to
possibly get my issue resolved? Thank you for your time and
consideration. attachments enclosed that can be opened in notepad.
Bit of code that is a member function of the user defined struct follows:

Code:
 void token::newTable(token oldTable[]) 
{
ifstream input;
input.open("text.txt", ios::in);
 
if (!input)
	{
	cerr<<"Error opening file:" <<endl;
	return;
	}
token newTable[5];
int codeCntr=3;
for(int j=0;j<=codeCntr;j++){
cout <<oldTable[j].key <<endl;
	newTable[j]=oldTable[j];
cout <<newTable[j].code <<endl;}
int found=0;
int k=0, h=0, first_time=1;//, loopCntr=1;
first_time=0;
//char current, next;
char c[20]="";
char tempString[20]="";
char buffer[20]="";
// char *emptyArray[20]="	 ";
ofstream outdata;
outdata.open("outdata.txt");
//buffer[30]=emptyArray[30];
//cout<<buffer<<"<---intial buffer"<<endl;
input.get(*tempString);
strcat(buffer,tempString);
while(!input.eof())
	{
cout<< newTable[codeCntr].key<<endl<<endl;
//for(int m=0;m<1;m++)
//{
//buffer=emptyArray;
cout<< newTable[codeCntr].key<<endl;
//}
//for(int m=0;m<5;m++)
//buffer[m]=0;
input.get(*c);
//	 strcat(buffer,tempString);
	 strcat(buffer,c);
 
cout <<c <<"<---c value" <<endl;
cout <<buffer<<"<--buffer value"<<endl;
 
cout <<tempString <<"<---tempString"<<endl;
found=0;
for(k=0;k<=codeCntr;k++)
{
usleep(1600);
cout <<newTable[k].key <<"<---newTable character"<<endl;;
cout <<buffer<<" b4 if statement" <<endl;
 
if(char(newTable[k].key)==char(buffer)&&found==0)
{
		tempString=buffer;
		cout<<"found it**************" <<endl<<k<<endl;
for(int m=0;m<1;m++)
buffer[m]=0;
found=1;
//for(int l=0;l<10;l++)
// buffer[30]=emptyArray[30];
// strcpy(tempString,"	");
}
}
if(found==0) 
{
h=0;
codeCntr++;
//char bufferHolder[20]=" ";
 
//bufferHolder=buffer;
	 newTable[codeCntr].code=codeCntr;
cout<<newTable[codeCntr].code<<" newTable code"<<endl;
cout<< (newTable[codeCntr].key=buffer)<<endl;
cout<< newTable[codeCntr].key<<endl;
strcpy(buffer,"\0");
tempString=c;
cout<<tempString<<"<--tempString in no found" <<endl;
cout<<c<<"<--c value" <<endl;
cout<<codeCntr<<" = newTable.code"<<endl;
//for(int m=0;m<5;m++)
//{
//buffer=emptyArray;
cout<< newTable[codeCntr].key<<endl;
cout<<buffer<<"buffer after delete"<<endl;
cout<< newTable[codeCntr].key<<endl;
 
cout<<"in !found statement" <<endl;
}
 
	}//end first while loop
 
//for(int h=0;h<3;h++)
//cout<< newTable[h].key;
input.close();
outdata.close();
return; 
}
 
Attached Files
File Type: txt catZip.txt (7.5 KB, 309 views)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Issue implementing algorithm of Lempel Ziv


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway