c++ - Error In string comparsion, Bad Excess c -
i'm trying read file , determine type of calculation perform. read in type string. later use else if (euc.compare(type) == 0)
compare it. doesn't work.
here code:
ifstream infile; infile.open(filename.c_str()); // first readin file calcuate on // define needed parameters string word1, word2, word3; string outtype; string type = "tbd"; string euc = "euc_2d"; // want know dimension have int givendimension; int dimen = 0; cout<<endl<<endl; cout<<"this "<<filename<<endl; cout<<"the information given is:"<<endl; // read file , determine dimension , type while(infile >> word1) { //cout<<word1<<endl; if (word1 == "dimension") { infile>> word2 >> givendimension; dimen = givendimension; // dimensional obtianed } if (word1 == "dimension:") { infile>>givendimension; dimen = givendimension; // dimensional obtianed } if (word1 == "edge_weight_type"){ infile>> word2 >> outtype; type = outtype; } if (word1 == "edge_weight_type:"){ infile>> outtype; type = outtype; } if (word1 == "node_coord_section") { break; } if (word1 == "node_coord_section:") { break; } } cout<<"dimenson "<<dimen<<endl; cout<<"type "<<type<<endl; cout<<dimen<<endl; cout<<type<<endl; // n*n matrix contain each city each city distance int allcitydistance[(dimen)*(dimen)]; if (type == "geo") { // data int x; // index of city double y,z; // use output data double latitude[dimen]; // define latitude number of dimension double longitude[dimen]; // define longitude number of dimension while (infile >> x >> y >> z) { // obtain y , z given format // convert latitude , longitude int index = x-1; latitude[index] = latitudeandlongitude(y); longitude[index] = latitudeandlongitude(z); //printf("city %d has %f , %f\n",x,latitude[index],longitude[index]); } int check[dimen][dimen]; (int = 0; < dimen; i++) { //cout<<i<<" "; (int j = 0; j < dimen; j++) { check[i][j] = abs(caldistance(latitude[i], longitude[i], latitude[j], longitude[j])); allcitydistance[dimen*(i) + j] = check[i][j]; //cout<<check[i][j]<<" "; } //cout<<endl; } } else if (euc.compare(type) == 0){ // error appear above // data int x; // index of city double y,z; // use read in data double xdirection[dimen], ydirection[dimen]; while (infile >> x >>fixed >> y >>fixed >> z) { // obtain y , z given format // convert latitude , longitude //cout<<y<<" "<<z<<endl; int index = x-1; xdirection[index] = (y); ydirection[index] = (z); //printf("city %d has %f , %f\n",x,xdirection[index],ydirection[index]); } double xdifference = 0.0, ydifference = 0.0; int check[dimen][dimen]; //check[1][1] = 0; (int = 0; < dimen; i++) { //cout<<i<<" "; (int j = 0; j < dimen; j++) { if (i == j) { check[i][i] = 0; }else{ xdifference = xdirection[i] - xdirection[j]; ydifference = ydirection[i] - ydirection[j]; //cout<<i<<" "<<xdifference<<" "<<ydifference; check[i][j] = round(sqrt(xdifference*xdifference + ydifference*ydifference)); } allcitydistance[dimen * + j] = round(sqrt(xdifference*xdifference + ydifference*ydifference)); //cout<<check[i][j]<<" "; } //cout<<endl; } }
i keep getting error:
operator==(const basic_string<_chart,_traits,_allocator>& __lhs, const _chart* __rhs) _noexcept{ return __lhs.compare(__rhs) == 0; }
exc_bad_access
it weird me.
from error logs, i'd 1 or more instances of use of == operator causing problem. usually, exc_bad_access happens when access area of memory not allocated. looking @ conditionals in code, place faulty memory access happen compare word1
against various parameters.
you seem have pre-initialized fine, there shouldn't bad accesses. word1
variable looks suspicious.
are sure being read file? file formatted properly?
Comments
Post a Comment