473,508 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access Violation----Why is that?

stealwings
34 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. // SpanishBreakPredict.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7. #include <fstream>
  8. using std::ifstream;
  9. using std::ofstream;
  10. #include <stdio.h>
  11. #include <cstdlib>
  12. #include <string>
  13. #include <stdlib.h>
  14. #include <iomanip>
  15. #include <vector>
  16. #define MAXLENGTH 1000
  17.  
  18.  
  19. int wmain(int argc, wchar_t * argv[], wchar_t *envp[])
  20. {
  21.     char WordCount[MAXLENGTH];        //Temp array to save input file words for lenght calculation
  22.     char MyWord[MAXLENGTH];            //Temp array to save input file words.
  23.     char MyStandard[MAXLENGTH];        //Temp array to save the standard words list
  24. //    vector<string> MyWords;            // Vector to hold input file.
  25. //    vector<string> Standard;        // Vector to hold Text-normalization-phrases
  26.     ifstream CountStandard;
  27.     ifstream CountInput;
  28.     ifstream ReadWord;
  29.     ifstream ReadStandard;
  30.     ofstream WriteWord;
  31.  
  32. /**********************To get number of characters of each line****************************/
  33.     int StandardSize=0;
  34.     CountStandard.open("Standard.txt");
  35.     while(!CountStandard.eof()&&CountStandard.getline(WordCount, MAXLENGTH))
  36.     {
  37.         StandardSize++;
  38.     }
  39. //    cout<<StandardSize<<endl;
  40.     CountStandard.close();
  41.     vector<string> Standard(StandardSize);
  42.     int InputSize=0;
  43.     CountInput.open("file.txt");
  44.     while(!CountInput.eof()&&CountInput>>WordCount)//CountInput.getline(WordCount, MAXLENGTH))
  45.     {
  46.         InputSize++;
  47.     }
  48.     cout<<InputSize<<endl;
  49.     vector<string> MyWords(InputSize);
  50.  
  51. /**************************************************************************************/
  52.     WriteWord.open("file.dat",ios::app);
  53.     if(!WriteWord.is_open())
  54.     {
  55.         cerr<<"couldn't create file"<<endl;
  56.         exit(1);
  57.     }
  58.     ReadStandard.open("Standard.txt");    
  59.     if (!ReadStandard)
  60.     {
  61.         cerr<<"Standard text file not found."<<endl;    
  62.         exit (1);
  63.     }
  64.  
  65.  
  66.     while(!ReadStandard.eof())                        //read the file till reaches the end.
  67.     {
  68.  
  69.         ReadStandard.getline(MyStandard,MAXLENGTH);      // read a line from the file.
  70.                                                      //    (one line is a phrase)
  71.         Standard.push_back(MyStandard);    //push the phrase into the vector "Standard";
  72.  
  73.     }                                                //read standard words over
  74.     /*for(unsigned i=0; i<Standard.size();i++)
  75.         {
  76.             cout<<Standard[i]<<endl;
  77.         }*/
  78.     ReadStandard.close();//close the file handle;
  79.  
  80.  
  81.     ReadWord.open("file.txt");
  82.     if (!ReadWord)
  83.     {
  84.         cerr<<"input file not found please ensure it exist."<<endl;
  85.         exit (1);
  86.     }
  87.  
  88.  
  89.     while(ReadWord>>MyWord)
  90.     {
  91.     //    cout<<MyWord<<endl;    
  92.     //    ReadWord>>MyWord;
  93.     //    ReadWord.getline(MyWord,MAXLENGTH);
  94.         MyWords.push_back(MyWord);    
  95.     }
  96.  
  97. /*******************************To print holding words of vector**********************/
  98. /*    for(int i=0; i<MyWords.size(); i++)
  99.     {
  100.         cout<<MyWords[i]<<endl;
  101.     }*/
  102. /**************************************************************************************/
  103.     ReadWord.close();
  104.     WriteWord.close();
  105.  
  106. /**************************************************************************************/
  107. /*                    SEARCH AND COMPARE, THEN REPLACE IF MATCH IS TRUE.                */
  108. /*                        Note: This part does a linear search                          */
  109. /**************************************************************************************/
  110. /*    for(unsigned i=0;i<Standard.size();i++) 
  111.     {
  112.  
  113.         for(unsigned j=0;j<MyWords.size();j++)  //each MyWords compare to the Standard 
  114.                                                 //and see if it is in the standard vector
  115.         {
  116.             string::size_type FoundAt=MyWords[i].find(Standard[j]);
  117.             while( string::npos != FoundAt )
  118.                 {
  119.                     MyWords[i].replace( FoundAt, Standard[j].length(),Standard[j]+"/");
  120.                     //cout<< MyWords[i]<< endl;
  121.                     FoundAt = MyWords[i].find( Standard[j], FoundAt + Standard[j].length() );
  122.                 }
  123.  
  124.             //if(MyWords[i]==Standard[j])            // if it equal to one of the words in the standard vector 
  125.             //    MyWords[i]=MyWords[i]+"/";            // add the slash to the end;
  126.         }
  127.  
  128.     }*/
  129.  
  130.  
  131. /*********************************************************************************************/
  132. /*                                    BinarySearch                                              */
  133. /*********************************************************************************************/
  134. int first=0, last, mid;
  135. while(Standard[first]<=Standard[last])
  136. {
  137.     Standard[mid]=Standard[(first+last)/2];
  138.     for (int i=0; i<MyWords.size(); i++)
  139.     {
  140.         if(MyWords[i]<Standard[mid].substr())
  141.             first=mid+1;        
  142.  
  143.     }
  144. }
  145. /***********************************Create output file****************************************/
  146. /*    for(unsigned j=0;j<MyWords.size();j++)        // for each word in MyWords vector  
  147.     {
  148.         WriteWord<<MyWords[j]<<endl;            // write it to the destination file
  149.     }
  150.     WriteWord.flush();                            // flush the memory to ensure all is written to the file
  151.     WriteWord.close();    */                        // close the write file handls
  152.     return 0;
  153. }
  154.  
THAT IS MY CODE AND THIS IS THE PART THAT IS GIVING ME TROUBLE
Expand|Select|Wrap|Line Numbers
  1. int first=0, last, mid;
  2. while(Standard[first]<=Standard[last])
  3. {
  4.     Standard[mid]=Standard[(first+last)/2];
  5.     for (int i=0; i<MyWords.size(); i++)
  6.     {
  7.         if(MyWords[i]<Standard[mid].substr())
  8.             first=mid+1;        
  9.  
  10.     }
  11. }
Apr 10 '07 #1
1 1516
stealwings
34 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. int first=0;
  2. int last= StandardSize-1;
  3. int mid=(first+last)/2;
  4. //cout<<MyWords.size()<<endl;
  5.     for (int i=0; i<MyWords.size();i++)
  6.     {
  7.         while(first<=last)
  8.         {
  9.             if(MyWords[i]>Standard[mid].substr())
  10.                 first=mid+1;
  11.             else if(MyWords[i]<Standard[mid].substr())
  12.                 last=mid-1;
  13.             else 
  14.                 cout<<Standard[mid]<<endl;
  15.         }
  16.         cout<<"Not found"<<endl;
  17.         return -1; 
  18.     }
  19.  
it gives me an infinite loop of blank spaces, why?
Apr 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1255
by: Torsten Mohr | last post by:
Hi, in the documentation and the examples that describe how to make the members of a new type accessible it says that i need to use the getattrfunc and setattrfunc if i want to access members...
6
2062
by: jameshamilton777 | last post by:
I want to make my website properly accessible. I want to assign keys that navigate to a different page when pressed. I've looked into the accesskey attribute, but under IE that involves pressing...
1
1838
by: John Baker | last post by:
HI: Does anyone know a good manual or web site that addresses the development of applications using the Data Access Capability (HTM) in Access. I need to have something on a web site that, at...
1
3403
by: Zvonko Tusek | last post by:
Is there a way to use access database on web host (I have a web hosting account) in visual basic. I want to make Visual basic client applications that connect to internet and work with access...
2
1230
by: freddy | last post by:
How can I connect to a access db that links to an excel spreadsheet and display the data in a datagrid
4
1117
by: THY | last post by:
Hi, I have create a connection to an access database, but before I close the connection, another user cannot run the web application correctly, I got the error Operation is not allowed when the...
0
1266
by: F I S H | last post by:
The site I am going to build has roughly 1000 html, php, asp, mp3, pdf, swf, doc. jpg, txt files etc....... I put them in 100 different folders. 200 users will be given access to 10 folders...
2
2292
by: k-man | last post by:
Hi: I have an MS Access query for a table called MyTable. One of my fields in the query is a custom field that looks like "MyField: = MyFunction(ID)" where ID is a field in MyTable. I have...
6
4938
by: adversus | last post by:
Good afternoon folks: I am maintaining an Access application (in the form of a compiled ADP project, *.ade) that currently gets installed on client machines via a Windows VBScript. Right now when...
1
1376
by: ken estes | last post by:
Have an older access program that I loaded to a mass storage device to upload into my laptop (Vista). When I try to initiate the Access program a display comes up saying I need to reformat the older...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.