【问题标题】:ifstream getline of string into Array?ifstream getline 将字符串转换为数组?
【发布时间】:2015-07-19 08:49:27
【问题描述】:

我正在尝试使用此功能

int input(int marks[classMax][3], string names[classMax], float& avg)
{
    for (int i = 0; i < students; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            fin >> marks[i][j];
        }
        fin >> names[i];
    }
}

将带有学生姓名的分数列表放入两个数组中。名单如下: M1 M2 M3 FirstName LastName,其中 M 表示标记。 循环工作正常,但是当它到达名字和姓氏之间的空间时,程序似乎只将名字写入数组。我尝试使用fin.getfin.getline(),但出现此错误:

error: no matching function for call to 'std::basic_ifstream<char>::get(std::strings&, int)'

【问题讨论】:

    标签: c++ fstream ifstream getline


    【解决方案1】:

    你可以试试这个:

    int input(int marks[classMax][3], string names[classMax], float& avg)
    {
        for (int i = 0; i<students; i++)
        {
            for (int j = 0; j<3; j++)
            {
                fin >> marks[i][j];
            }
            std::getline(fin, names[i]);
        }
    }
    

    欲了解更多信息,请查看cppreference

    【讨论】:

      【解决方案2】:

      std::basic_istream::getline 不接受std::string

      std::getline 是您想与std::string 一起使用的内容。因为它是一个非成员函数模板,所以调用它时输入流作为第一个参数,输出字符串作为第二个参数:

      std::getline(fin, names[i]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-27
        • 1970-01-01
        • 2011-06-18
        • 2017-01-12
        相关资源
        最近更新 更多