Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 第一个索引后未填充结构数组索引_C++_Arrays_Loops_Struct_Fstream - Fatal编程技术网

C++ 第一个索引后未填充结构数组索引

C++ 第一个索引后未填充结构数组索引,c++,arrays,loops,struct,fstream,C++,Arrays,Loops,Struct,Fstream,我有一个输入文件,每行有3个字段,类型为:string、double、double。有15行数据 输入文件数据的格式为: 加德满都-34,28 城市名称、低温、高温 .... ... 很明显,根据输出,它没有在线路上获得第三个输入 代码如下: for (int index = 0; index < 15; index++) { getline(inFile, weatherInfo[index].city, ','); inFile >

我有一个输入文件,每行有3个字段,类型为:string、double、double。有15行数据

输入文件数据的格式为:

加德满都-34,28
城市名称、低温、高温
....
...

很明显,根据输出,它没有在线路上获得第三个输入

代码如下:

for (int index = 0; index < 15; index++)
    {
        getline(inFile, weatherInfo[index].city, ',');
        inFile >> weatherInfo[index].low >> weatherInfo[index].high;
        inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');   
    }
我知道我的程序能够读取其他行,因为当我添加

inFile.ignore(20);
在我语句的开头,它是它输出的循环

28
Perth (92, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
输出代码:

void ShowAll(int count)                                         //Show entire data function
{

int x = 0;                                                  //loop through the index of city[], lowTemp[], highTemp[], and print them.
while (x < count)
{
    cout << weatherInfo[x].city << " (" << weatherInfo[x].low << ", " << weatherInfo[x].high << ")" << endl;

    x++;

}

cout << endl;
}
void ShowAll(int count)//显示整个数据函数
{
int x=0;//循环遍历城市[]、低温[]、高温[]的索引并打印它们。
while(xcout如果一行中的数据用逗号分隔,则应使用以下方法

#include <sstream>

//...

std::string line;

for ( int index = 0; index < 15 && std::getline( inFile, line ); index++)
{
    std::istringstream is( line );

    getline( is, weatherInfo[index].city, ',');

    std::string field;
    if ( getline( is, field, ',') ) weatherInfo[index].low = std::stod( field );
    if ( getline( is, field, ',') ) weatherInfo[index].high = std::stod( field );
}
#包括
//...
std::字符串行;
对于(int index=0;index<15&&std::getline(infle,line);index++)
{
std::istringstream是(行);
getline(is,weatherInfo[index].city,,);
std::字符串字段;
if(getline(is,field,,))weatherInfo[index].low=std::stod(field);
if(getline(is,field,,))weatherInfo[index].high=std::stod(field);
}
代码的问题是,当您尝试读取双值时发生错误,并且遇到逗号。在这种情况下,流的状态将是错误的,所有其他输入将被忽略


此外,您还应该检查您使用的区域设置中的double的点表示形式。

显示数据在输入文件中的存储方式。完成,很抱歉,我没有开始。可能是打印问题,而不是阅读问题?这里可能有输出打印代码也会很有用。添加了@RushWow输出打印代码。这就是问题所在。Th这是我的第二个C++,我从来没有见过这样的代码。
#include <sstream>

//...

std::string line;

for ( int index = 0; index < 15 && std::getline( inFile, line ); index++)
{
    std::istringstream is( line );

    getline( is, weatherInfo[index].city, ',');

    std::string field;
    if ( getline( is, field, ',') ) weatherInfo[index].low = std::stod( field );
    if ( getline( is, field, ',') ) weatherInfo[index].high = std::stod( field );
}