GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Nov-2004, 08:23
wbsquared03 wbsquared03 is offline
New Member
 
Join Date: Oct 2004
Posts: 14
wbsquared03 is on a distinguished road

won't read file from disk, dunno why


anyone have any idea why this won't read in the file from disk....im guessing i made a stupid mistake somewhere. Any help would be appreciated

CPP / C++ / C Code:
//////////////////////////////////////
////////////Sean Walsh///////////////
////////////////////////////////////

#include <iostream>
#include <fstream>	//opening files
#include <string>	//used for strings
#include <cstddef>	//used for NULL command
using namespace std;

struct NodeType;

struct NodeType		//doubly linked list
{
	string word;
	int freq;
	NodeType* prev;
	NodeType* next;
};

class SortedType
{
public:
	SortedType();
	bool IsEmpty() const;
	int LengthIs() const;
	void FindItem(string& word, int& freq, bool& found);
	void InsertFirst(string newword);
	void InsertItemAfter(string newword);
	void IncFreq();
	void DeleteItem(string word, int& freq);
	void ResetList();
	void GetNextItem(string& word, int& freq, bool& moreNode);
private:
	NodeType *listData;
	int length;
	NodeType* currentPos;
};

SortedType::SortedType()
{
	length=0;
	listData=NULL;
	currentPos=NULL;
}

bool SortedType::IsEmpty() const
{
	return (length==0);
}

int SortedType::LengthIs() const
{
	return length;
}

void SortedType::FindItem(string& word, int& freq, bool& found)
{
	bool moreToSearch;
	NodeType* preLoc;

	preLoc=NULL;
	currentPos=listData;
	moreToSearch=(currentPos!=NULL);
	found=false;

	while(moreToSearch)
	{
		if((currentPos->word)<word)
		{
			preLoc=currentPos;
			currentPos=currentPos->next;
			moreToSearch=(currentPos!=NULL);
		}
		else
		{
			moreToSearch=false;
		}
	}

	if(currentPos==NULL)				//all the words are smaller than search word
	{
		currentPos=preLoc;
	}
	else if(currentPos->word==word)		//found
	{
		found=true;
	}
	else if(currentPos==listData)		//first node is larger than newword
	{
		currentPos=NULL;
	}
	else								//for insertAfter, move one back.
	{
		currentPos=currentPos->prev;
	}
}

void SortedType::InsertFirst(string newword)
{
	//The current list is empty

	NodeType* newNode;

	newNode= new NodeType;
	newNode->word=newword;
	newNode->prev=NULL;
	newNode->next=NULL;
	newNode->freq=1;
	listData=newNode;
}

void SortedType::InsertItemAfter(string newword)
{
	NodeType* newNode;
	NodeType* cpos;
	
	newNode=new NodeType;
	newNode->word=newword;
	newNode->freq=1;

	if(currentPos!=NULL)						//case II and III
	{
		newNode->prev=currentPos;
		newNode->next=currentPos->next;

		if(currentPos->next!=NULL)				//case II
			currentPos->next->prev=newNode;
		
		currentPos->next=newNode;
	}
	else										//case I
	{
		newNode->prev=NULL;
		newNode->next=listData;
		listData->prev=newNode;
		listData=newNode;
	}
	length++;

	//Just for Testing the generation list

	cpos=listData;
	cout<<"Current list is: ";
	while(cpos!=NULL)
	{
		cout<<cpos->word<<" "<<cpos->freq<<", ";
		cpos=cpos->next;
	}
	cin.get();
	cout<<endl;
	
}

void SortedType::IncFreq()
{
	(currentPos->freq) ++;
}

void SortedType::DeleteItem(string word, int& freq)
{


}

void SortedType::ResetList()
{
	currentPos=NULL;
}

void SortedType::GetNextItem(string& word, int& freq, bool& last)
{
	bool moreNode=true;

	//A copy of the next word and freq in the list is returned
	//when the end of list is reached, currentPos is rest to begin
	//and more is false

	if(currentPos==NULL)
	{
		moreNode=false;
		currentPos=listData;
	}
	else
	{
		currentPos=currentPos->next;
		word=currentPos->word;
		freq=currentPos->freq;
		moreNode=true;
	}
}

//Client Code
int main()
{
    SortedType wlist;
	string readword;
	ifstream inF;	//the data file "book.txt"
	ofstream outF;
	bool exist;
	int frequency;
	int selection;	//integer of menu option
	
	
	
	inF.open("a:\book.txt");
	if(!inF)
	{
		cout<<"ERROR! Can't open input file"<<endl;
		return 1;
	}
	inF>>readword;
	while(readword[0]<'A'||readword[0]>'Z')
	{
		inF>>readword;
	}
	cout<<"new word: "<<readword<<endl;
	cin.get();
	wlist.InsertFirst(readword);

	while(!inF.eof())
	{
		inF>>readword;
		while(readword[0]<'A'||readword[0]>'Z')
		{
			inF>>readword;
		}
		cout<<"next word is "<<readword<<endl;
		wlist.FindItem(readword, frequency, exist);
		if(!exist)
		{
			wlist.InsertItemAfter(readword);
		}
		else
		{
			wlist.IncFreq();
		}
	}
	
	//The number of words in the list
	cout<<"The number of nodes in the list is "<<wlist.LengthIs()<<endl;

	//menu
	cout<<"********************"<<endl;
	cout<<"		 <Menu> 	   "<<endl;
	cout<<"********************"<<endl;
	cout<<"1.) Enter the word you want to search"<<endl;
	cout<<"2.) Enter the word you want to delete"<<endl;
	cout<<"3.) Enter the word you want to insert"<<endl;
	cout<<"4.) List all the words with their frequency"<<endl;
	cout<<"5.) Exit"<<endl;
	cin>>selection;
	//end menu

	while(selection!=999)
	{
		switch(selection)
		{
		case 1: cout<<"Enter in the word you want to search for: "<<endl;
            cin>>readword; 
			wlist.FindItem(readword,frequency,exist);
			break;
		case 2: cout<<"Enter in the word you want to delete: "<<endl;
			cin>>readword; 
			wlist.DeleteItem(readword,frequency);
			break;
		case 3: cout<<"Enter in the word you want to insert: "<<endl;
			cin>>readword;
			wlist.InsertItemAfter(readword);
			break;
		case 4: 
			break;
		case 5: cout<<"GoodBye"<<endl;
			selection=999;
			break;
		}
	}
	return 0;

}
  #2  
Old 29-Nov-2004, 08:29
wbsquared03 wbsquared03 is offline
New Member
 
Join Date: Oct 2004
Posts: 14
wbsquared03 is on a distinguished road
Quote:
Originally Posted by wbsquared03
im guessing i made a stupid mistake somewhere. Any help would be appreciated

As i guessed i made stupid mistake i used a backslash instead of a foward slash....wow my dumbass sucks. Iv been looking for why for 3 days....what a rookie mistake HAHA.
  #3  
Old 29-Nov-2004, 09:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by wbsquared03
As i guessed i made stupid mistake i used a backslash instead of a foward slash....wow my dumbass sucks. Iv been looking for why for 3 days....what a rookie mistake HAHA.
Been there, done that. Often!!!

By the way, if you are having a problem and pinpoint it to a specific statement (or area of your program) it would be better to post a few lines around that statement rather than 280 lines of code that we have to search thru to find it. It's easier on us and gets you an answer faster. If we need more, we'll ask for it.
__________________

Age is unimportant -- except in cheese
  #4  
Old 29-Nov-2004, 11:19
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I would've said that your backslash instead of a forward slash problem was actually just a missing backslash problem. Your code has

CPP / C++ / C Code:
inF.open("a:\book.txt");
This should instead be written as

CPP / C++ / C Code:
inF.open("a:\\book.txt");
The reason for the double-backslash being that '\b' is a character, but it's not the one you want in that string. Adding another backslash before it tells the compiler that it's a backslash, not a '\b'.
__________________
-Aaron
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect end of file with read() function call? nkhambal C Programming Language 6 12-Oct-2004 01:08
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.