簡體   English   中英

ifstream getline of string into Array?

[英]ifstream getline of string into Array?

我正在嘗試使用此功能

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表示Mark。 循環工作正常但當它到達First和Last Name之間的空間時,程序似乎只將FirstName寫入數組。 我嘗試使用fin.getfin.getline()但我收到此錯誤:

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

你可以試試這個:

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

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

std::getline是你想要用於std::string 由於它是非成員函數模板,因此使用輸入流作為第一個參數並將輸出字符串作為第二個參數調用它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM