C++ 表操作错误
我试图将一个变量设置为2D数组元素的值。这应该很容易,但每当我写它时,变量中都会得到0。我读入了一个文件以手动放入数组元素,这可能是导致问题的原因,但我不确定出了什么问题 这是我的密码:C++ 表操作错误,c++,C++,我试图将一个变量设置为2D数组元素的值。这应该很容易,但每当我写它时,变量中都会得到0。我读入了一个文件以手动放入数组元素,这可能是导致问题的原因,但我不确定出了什么问题 这是我的密码: int main(){ //declaring all the variables vector < vector <double> > data; // vector of vectors to hold the data. int z=0; int z1,z2; //open th
int main(){
//declaring all the variables
vector < vector <double> > data; // vector of vectors to hold the data.
int z=0;
int z1,z2;
//open the input file as an input file stream (ifstream)
ifstream dataFile;
dataFile.open( "GBplaces.csv" );
//if the file is open...
if (dataFile.is_open()) {
// and while it's not the end of the file...
while (!dataFile.eof() ){
z++;
if (z==1){
string aLine;
getline ( dataFile, aLine );
printf ("place, type, population, lattitude, longitude \n\n");
}
else if(z==102){
string aLine;
getline ( dataFile, aLine );
}
else {
//read in a line of the file and put the ontents into the arays.
string aLine; // hold the read in line
getline ( dataFile, aLine ); //reads line from dataFile to aLine
//split up the string aLine based on where the comma is
int comma;
int nextCom=0;
//comma_pos = aLine.find(',',0); //finds where the comma is, starting from the begining ie 0
string xst, yst, zst; //temp variables for column
vector <double> temp; // tempory vector for the points
double xt, yt, zt;
comma = aLine.find(',',0); //find first position of comma
xst = aLine.substr(0,comma); // extracts string subvalue
temp.push_back(atof(xst.c_str())); //add value to end of temporary vector
comma += 1; // add 1 to the comma position count
while (aLine.find(',',comma) != -1) {
// find middle values
if(aLine==""){}
else{
nextCom = aLine.find(',',comma);
yst = aLine.substr(comma, nextCom - comma);
temp.push_back(atof(yst.c_str())); //convert string into double and add it to the end of the vector
comma = nextCom + 1;
}
}
// same calculations as in the loop but for the last value after the comma
zst = aLine.substr(nextCom + 1, aLine.length());
temp.push_back(atof(zst.c_str()));
// push temporary vector onto data vector
data.push_back(temp);
}
}
}
intmain(){
//声明所有变量
vectordata;//用于保存数据的向量向量的向量。
int z=0;
int z1,z2;
//将输入文件作为输入文件流(ifstream)打开
ifstream数据文件;
dataFile.open(“GBplaces.csv”);
//如果文件已打开。。。
if(dataFile.is_open()){
//虽然这不是文件的结尾。。。
而(!dataFile.eof()){
z++;
如果(z==1){
弦线;
getline(数据文件,aLine);
printf(“地点、类型、人口、纬度、经度\n\n”);
}
else如果(z==102){
弦线;
getline(数据文件,aLine);
}
否则{
//读入文件中的一行,并将内容放入Aray中。
字符串aLine;//保持读入行
getline(数据文件,aLine);//从数据文件到aLine读取行
//根据逗号的位置拆分字符串aLine
int逗号;
int-nextCom=0;
//逗号_pos=aLine.find(',',0);//查找逗号的位置,从ie 0开始
字符串xst,yst,zst;//列的临时变量
vector temp;//点的时间向量
双xt、yt、zt;
逗号=aLine.find(',',0);//查找逗号的第一个位置
xst=aLine.substr(0,逗号);//提取字符串子值
temp.push_back(atof(xst.c_str());//将值添加到临时向量的末尾
逗号+=1;//将1添加到逗号位置计数
while(aLine.find(',,逗号)!=-1){
//找到中间值
如果(aLine==“”){}
否则{
nextCom=aLine.find(“,”,逗号);
yst=aLine.substr(逗号,nextCom-逗号);
temp.push_back(atof(yst.c_str());//将字符串转换为double并将其添加到向量的末尾
逗号=nextCom+1;
}
}
//与循环中的计算相同,但用于逗号后的最后一个值
zst=aLine.substr(nextCom+1,aLine.length());
温度后推(atof(zst.c_str());
//将临时向量推到数据向量上
数据。推回(温度);
}
}
}
z=数据[3][3]
当我运行程序时,我得到z=0,尽管当我打印表格时,元素是51.26。更新:
发现错误:
int z=0;
int z1,z2;
z1,z2应该是双精度的,而不是整数。这会弄乱数字的其余部分这:
而(!dataFile.eof())
几乎肯定是个问题。事实上,没有任何错误检查,这并没有多大帮助。非常感谢,我会看一看!