0

When I run the code with the first input file the code works fine, but when i use the second input file Dev-C++ 4.9.9.2 compiler, shows me the warning: "An Access Violation (Segmentation Fault) raised in your program" pointing to the line:

__test = __grouping_tmp[__i] == __grouping[__min];

of the file locale_facets.tcc of the compiler. I've searched to find possible solutions but nothing came up, can anybody assist me?

Here's My Code:

#include <algorithm>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

vector< vector<int> > adj;
vector <string> towns;
vector <int> w;//weights
vector <int> v;//visited
string town1,town2;
int n,i,j,k,l;

void dfs(int start){
int c,size;
v[start]=1;
size=adj[start].size();
for (c=0;c<size;c++)
   //if ((adj[start][c]==1) && (v[c]==0))
   //if (v[c]==0)
         dfs(adj[start][c]);
}
int findi(string name){ //Finds the I of the town
    for(i=0;i<towns.size();i++){
       if(name==towns[i]){
        return i;
    }
}
return 101;//Dead END
}

int main(){
ifstream fin ("B.in");
ofstream fout ("B.out");

towns.push_back("Start");
w.push_back(0);
bool found;
while(!fin.eof()){
    fin>>town1>>k>>town2>>l;
    found=false;
    for(i=0;i<towns.size();i++){//search an ksanabike i poli
        if(towns[i]==town1){
            found=true;
        }
    }
    if(!found){
        towns.push_back(town1);
        w.push_back(k);
    }
}
towns.push_back("Finish");
w.push_back(10001);
for(i=1;i<towns.size()-1;i++){//sorting by weight
    for(j=i+1;j<towns.size();j++){
        if(w[i]>w[j]){
            k=w[i];
            w[i]=w[j];
            w[j]=k;
            town1=towns[i];
            towns[i]=towns[j];
            towns[j]=town1;
        }
    }
}
int c;
string prev;
c=0;
prev=towns[0];
for(i=0;i<towns.size();i++){//sort towns with same weight
    if(towns[i]==prev){
        c++;
    }
    if(towns[i]!=prev&&c!=0){
        sort(towns.begin()+i-c-1,towns.begin()+i-1);
    }
    //Arxikopiisi visited je grafou
    v.push_back(0);
    adj.push_back(vector<int> ());
}
fin.clear();//reset file read!!
fin.seekg(0, std::ios::beg);
while(!fin.eof()){//Dimiourgia grafou
    fin>>town1>>k>>town2>>l;
    //cout<<town1<<" "<<town2<<endl;
    adj[findi(town1)].push_back(findi(town2));
}
dfs(0);
//cout<<"v size:"<<v.size();
for(i=1;i<towns.size()-1;i++){
    if(v[i]==1){
        fout<<towns[i]<<endl;
    }
}

fin.close();
fout.close();
return 0;
}

Input file that works-->

Start 0 Larnaca 40

Paphos 70 Finish 10001

Nicosia 90 Finish 10001

Larnaca 40 Paphos 70

Limasol 50 Paphos 70

Limasol 50 Larnaca 40

Nicosia 90 Limasol 50

Input File that gives the error -->

Paphos 40 Finish 10001

Nicosia 90 Finish 10001

Start 0 Larnaca 40

Larnaca 40 Paphos 40

Limasol 40 Larnaca 40

Larnaca 40 Limasol 40

Nicosia 90 Limasol 40

  • Note: [this `while(!fin.eof())` is **wrong**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Mar 26 '14 at 15:13
  • Running on gdb it shows an endless recursive call of dfs() function... More exactly is continually running dfs(2) that calls dfs(3) that calls dfs(2).... – jsantander Mar 26 '14 at 15:15
  • @user3464801 by following [the link in my comment](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) and reading the answers in the posted question. – WhozCraig Mar 26 '14 at 15:19
  • @WhozCraig Sorry, it's my first post and I didn't notice the link. – user3464801 Mar 26 '14 at 15:21
  • @user3464801 No worries. and note jsantander's comment. You apparently have an infinite recursion issue in your code. I cannot claim it is due to the double-read of the last input entry, but it certainly isn't helping. – WhozCraig Mar 26 '14 at 15:24
  • I found the problem, jsantander was right! Thanks @jsantander, can you recommend any guide to learn how run my programs on gdb? – user3464801 Mar 26 '14 at 15:35
  • @user3464801, I'm afraid I don't have any recommendations. Look at the [manual](http://www.gnu.org/software/gdb/documentation/). Googled for cheat sheet and found one that looked [concise enough](http://darkdust.net/files/GDB%20Cheat%20Sheet.pdf)... or try running it through an IDE (e.g. Eclipse CDT) or a gui (e.g. [ddd](https://www.gnu.org/software/ddd/) – jsantander Mar 26 '14 at 15:40

0 Answers0