C++ 字符串比较错误,错误超出c

C++ 字符串比较错误,错误超出c,c++,string,compare,C++,String,Compare,我正在尝试读取一个文件并确定要执行的计算类型。我将以字符串形式读入类型。稍后我将使用else if(EUC.compare(type)==0)对其进行比较。然而,它并不总是有效的 这是我的密码: ifstream infile; infile.open(fileName.c_str()); // First readin the file that we are to calcuate on // Define the needed parameters string word1, word2

我正在尝试读取一个文件并确定要执行的计算类型。我将以字符串形式读入类型。稍后我将使用
else if(EUC.compare(type)==0)
对其进行比较。然而,它并不总是有效的

这是我的密码:

ifstream infile;
infile.open(fileName.c_str());

// First readin the file that we are to calcuate on
// Define the needed parameters
string word1, word2, word3;
string outtype;
string type = "TBD";
string EUC = "EUC_2D";

// Also want to know what is the dimension we have
int givenDimension;
int dimen = 0;

cout<<endl<<endl;
cout<<"This is "<<fileName<<endl;
cout<<"The information we are given is:"<<endl;

// Read the file and determine the its dimension and 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 is  "<<dimen<<endl;
cout<<"Type is  "<<type<<endl;

cout<<dimen<<endl;
cout<<type<<endl;
// The n*n matrix that contain each city to each city distance
 int allcityDistance[(dimen)*(dimen)];

if (type == "GEO") {
// Now we get the data
int x;          // Index of city
double y,z;     // use to output data
double latitude[dimen];     // Define latitude with the number of dimension
double longitude[dimen];    // Define longitude with the number of dimension
while (infile >> x >> y >> z)
{
    // As we obtain y and z as given format
    // we convert into latitude and longitude
    int index = x-1;
    latitude[index] = latitudeAndlongitude(y);
    longitude[index] = latitudeAndlongitude(z);
    //printf("city %d has %f and %f\n",x,latitude[index],longitude[index]);

}

    int check[dimen][dimen];
    for (int i = 0; i < dimen; i++) {
        //cout<<i<<"  ";
        for (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


        // Now we get the data
        int x;          // Index of city
        double y,z;     // use to read in data
        double xdirection[dimen], ydirection[dimen];
        while (infile >> x >>fixed >> y >>fixed >> z)
        {   
            // As we obtain y and z as given format
            // we convert into latitude and longitude
            //cout<<y<<"  "<<z<<endl;
            int index = x-1;
            xdirection[index] = (y);
            ydirection[index] = (z);
            //printf("city %d has %f and %f\n",x,xdirection[index],ydirection[index]);
        }

        double xdifference = 0.0, ydifference = 0.0;
        int check[dimen][dimen];
        //check[1][1] = 0;
        for (int i = 0; i < dimen; i++) {
            //cout<<i<<"  ";
            for (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 * i + j] = round(sqrt(xdifference*xdifference + ydifference*ydifference));
                //cout<<check[i][j]<<"  ";
            }
            //cout<<endl;
        }
    }
ifstream-infle;
open(fileName.c_str());
//首先读入我们要计算的文件
//定义所需的参数
字符串word1、word2、word3;
字符串输出类型;
字符串类型=“待定”;
字符串EUC=“EUC_2D”;
//还想知道我们的维度是什么
内给定尺寸;
int-dimen=0;

cout从错误日志中,我认为是使用==运算符的一个或多个实例导致了问题。通常,当您访问未分配的内存区域时,会发生EXC_BAD_访问。查看代码中的所有条件,唯一可能发生错误内存访问的地方是将
word1
与各种参数进行比较

您似乎已经对所有内容进行了预初始化,所以不应该存在错误的访问
word1
是唯一看起来可疑的变量

您确定正在从文件中正确读取它吗?文件格式正确吗

operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
       const _CharT* __rhs) _NOEXCEPT{
  return __lhs.compare(__rhs) == 0;
 }