Access Violation----Why is that?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stealwings
    New Member
    • Feb 2007
    • 34

    Access Violation----Why is that?

    I have implemented a binary search and some other functions to my code, when I compile it, it gives me no error, but when I run it, it gives me an Access violation, why? Can some one explain me plz. Thanks in advance.
    Code:
    // SpanishBreakPredict.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include <stdio.h>
    #include <cstdlib>
    #include <string>
    #include <stdlib.h>
    #include <iomanip>
    #include <vector>
    #define MAXLENGTH 1000
    
    
    int wmain(int argc, wchar_t * argv[], wchar_t *envp[])
    {
    	char WordCount[MAXLENGTH];		//Temp array to save input file words for lenght calculation
    	char MyWord[MAXLENGTH];			//Temp array to save input file words.
    	char MyStandard[MAXLENGTH];		//Temp array to save the standard words list
    //	vector<string> MyWords;			// Vector to hold input file.
    //	vector<string> Standard;		// Vector to hold Text-normalization-phrases
    	ifstream CountStandard;
    	ifstream CountInput;
    	ifstream ReadWord;
    	ifstream ReadStandard;
    	ofstream WriteWord;
    
    /**********************To get number of characters of each line****************************/
    	int StandardSize=0;
    	CountStandard.open("Standard.txt");
    	while(!CountStandard.eof()&&CountStandard.getline(WordCount, MAXLENGTH))
    	{
    		StandardSize++;
    	}
    //	cout<<StandardSize<<endl;
    	CountStandard.close();
    	vector<string> Standard(StandardSize);
    	int InputSize=0;
    	CountInput.open("file.txt");
    	while(!CountInput.eof()&&CountInput>>WordCount)//CountInput.getline(WordCount, MAXLENGTH))
    	{
    		InputSize++;
    	}
    	cout<<InputSize<<endl;
    	vector<string> MyWords(InputSize);
    
    /**************************************************************************************/
    	WriteWord.open("file.dat",ios::app);
    	if(!WriteWord.is_open())
    	{
    		cerr<<"couldn't create file"<<endl;
    		exit(1);
    	}
    	ReadStandard.open("Standard.txt");	
    	if (!ReadStandard)
    	{
    		cerr<<"Standard text file not found."<<endl;	
    		exit (1);
    	}
    	
    	
    	while(!ReadStandard.eof())						//read the file till reaches the end.
    	{
    
    		ReadStandard.getline(MyStandard,MAXLENGTH);	  // read a line from the file.
    													 //	(one line is a phrase)
    		Standard.push_back(MyStandard);	//push the phrase into the vector "Standard";
    		
    	}												//read standard words over
    	/*for(unsigned i=0; i<Standard.size();i++)
    		{
    			cout<<Standard[i]<<endl;
    		}*/
    	ReadStandard.close();//close the file handle;
    
    
    	ReadWord.open("file.txt");
    	if (!ReadWord)
    	{
    		cerr<<"input file not found please ensure it exist."<<endl;
    		exit (1);
    	}
    	
    	
    	while(ReadWord>>MyWord)
    	{
    	//	cout<<MyWord<<endl;	
    	//	ReadWord>>MyWord;
    	//	ReadWord.getline(MyWord,MAXLENGTH);
    		MyWords.push_back(MyWord);	
    	}
       
    /*******************************To print holding words of vector**********************/
    /*	for(int i=0; i<MyWords.size(); i++)
    	{
    		cout<<MyWords[i]<<endl;
    	}*/
    /**************************************************************************************/
    	ReadWord.close();
    	WriteWord.close();
    
    /**************************************************************************************/
    /*					SEARCH AND COMPARE, THEN REPLACE IF MATCH IS TRUE.                */
    /*                        Note: This part does a linear search						  */
    /**************************************************************************************/
    /*	for(unsigned i=0;i<Standard.size();i++) 
    	{
    			
    		for(unsigned j=0;j<MyWords.size();j++)  //each MyWords compare to the Standard 
    												//and see if it is in the standard vector
    		{
    			string::size_type FoundAt=MyWords[i].find(Standard[j]);
    			while( string::npos != FoundAt )
    				{
    					MyWords[i].replace( FoundAt, Standard[j].length(),Standard[j]+"/");
    					//cout<< MyWords[i]<< endl;
    					FoundAt = MyWords[i].find( Standard[j], FoundAt + Standard[j].length() );
    				}
    		
    			//if(MyWords[i]==Standard[j])			// if it equal to one of the words in the standard vector 
    			//	MyWords[i]=MyWords[i]+"/";			// add the slash to the end;
    		}
    		
    	}*/
    
    	
    /*********************************************************************************************/
    /*									BinarySearch 											 */
    /*********************************************************************************************/
    int first=0, last, mid;
    while(Standard[first]<=Standard[last])
    {
    	Standard[mid]=Standard[(first+last)/2];
    	for (int i=0; i<MyWords.size(); i++)
    	{
    		if(MyWords[i]<Standard[mid].substr())
    			first=mid+1;		
    
    	}
    }
    /***********************************Create output file****************************************/
    /*	for(unsigned j=0;j<MyWords.size();j++)		// for each word in MyWords vector  
    	{
    		WriteWord<<MyWords[j]<<endl;			// write it to the destination file
    	}
    	WriteWord.flush();							// flush the memory to ensure all is written to the file
    	WriteWord.close();	*/						// close the write file handls
    	return 0;
    }
    THAT IS MY CODE AND THIS IS THE PART THAT IS GIVING ME TROUBLE
    Code:
    int first=0, last, mid;
    while(Standard[first]<=Standard[last])
    {
    	Standard[mid]=Standard[(first+last)/2];
    	for (int i=0; i<MyWords.size(); i++)
    	{
    		if(MyWords[i]<Standard[mid].substr())
    			first=mid+1;		
    
    	}
    }
  • stealwings
    New Member
    • Feb 2007
    • 34

    #2
    Sorry for the trouble, I already realized that it was a logical problem, it is solved, but I got this new problem, I have the part of the code as follow:
    Code:
    int first=0;
    int last= StandardSize-1;
    int mid=(first+last)/2;
    //cout<<MyWords.size()<<endl;
    	for (int i=0; i<MyWords.size();i++)
    	{
    		while(first<=last)
    		{
    			if(MyWords[i]>Standard[mid].substr())
    				first=mid+1;
    			else if(MyWords[i]<Standard[mid].substr())
    				last=mid-1;
    			else 
    				cout<<Standard[mid]<<endl;
    		}
    		cout<<"Not found"<<endl;
    		return -1; 
    	}
    it gives me an infinite loop of blank spaces, why?

    Comment

    Working...