【问题标题】:Issues with getline() and vectors (C++)getline() 和向量 (C++) 的问题
【发布时间】:2013-05-12 17:30:04
【问题描述】:

我正在尝试从文本文件中获取输入,并使用 getline() 将数据放入四个向量中。该文件包含两个字符串,一个双精度数和一个整数,都在不同的行上,每组由一个空行分隔。

    if (userChoice == 1) // Load
            {
                in_stream.open("Lab11.txt");
                if (in_stream.fail())
                {
                    cerr << "File does not exist" << endl;
                    system("PAUSE");
                    exit(1);
                }
                index = 0;
                do
                {
                    getline(in_stream, itemNumb[index]);
                    getline(in_stream, itemName[index]);
                    getline(in_stream, itemCost[index]);
                    getline(in_stream, itemQuant[index]);
                    index++;
                } while (! in_stream.eof());
                in_stream.close();
                itemStored = 0;
                cout << "Items stored: " << itemStored << endl;
            }

itemNumb 和 itemName 是字符串向量,itemCost 是双精度数,itemQuant 是整数。字符串的行没有给出错误,但是 double 和 integer 的行给出了相同的错误,类型根据它是哪个向量而变化。

错误:

没有匹配函数调用getline(std::ifstream&, double&)'|

任何帮助将不胜感激! 编辑:完整代码

    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <sstream>

    using namespace std;

    void Add(vector<string>& itemNumb, vector<string>& itemName, vector<double>& itemCost, vector<int>& itemQuant, string name, string numb, double cost, int quant, int length, int index);

    void Search(vector<string>& itemNumb, vector<string>& itemName, vector<double>& itemCost, vector<int>& itemQuant);

    void List(vector<string> itemNumb, vector<string> itemName, vector<double> itemCost, vector<int> itemQuant, int length, int index);

    int main()
    {
        ifstream in_stream;
        ofstream out_stream;
        vector<string> itemName, itemNumb;
        vector<double> itemCost;
        vector<int> itemQuant;
        string name = "000", numb = "000";
        double cost = 0.0;
        int quant = 0, length = 0, index = 0;
        int userChoice, itemStored;
        do
        {
            cout << "===========================" << endl << "1) Load" << endl << "2) Add" << endl <<
    "3) Search" << endl << "4) List" << endl << "5) Save" << endl << endl << "0) Exit" << endl;
            cout << "Choose an option" << endl;
            cin >> userChoice;
            if (userChoice == 1) // Load
            {
                in_stream.open("Lab11.txt");
                if (in_stream.fail())
                {
                    cerr << "File does not exist" << endl;
                    system("PAUSE");
                    exit(1);
                }
                index = 0;
                do
                {
                    getline(in_stream, itemNumb[index]);
                    getline(in_stream, itemName[index]);
                    in_stream >> itemCost[index];
                    in_stream >> itemQuant[index];
                    index++;
                } while (! in_stream.eof());
                in_stream.close();
                itemStored = itemNumb.size();
                cout << "Items stored: " << itemStored << endl;
            }
            else if (userChoice == 2) // Add
            {
                Add(itemNumb, itemName, itemCost, itemQuant, name, numb, cost, quant, length, index);
                itemStored++;  // Function seems to sort oddly when strings of varying lengths are compared
                cout << endl;
                cout << itemStored << " items stored" << endl;
                cout << endl;
            }
            else if (userChoice == 3) // Search
            {
                Search(itemNumb, itemName, itemCost, itemQuant);
            }
            else if (userChoice == 4) //List
            {
                List(itemNumb, itemName, itemCost, itemQuant, length, index);
            }
            else if (userChoice == 5) //Save
            {
                out_stream.open("Lab11.txt", ios::app);
                if (out_stream.fail())
                {
                    cerr << "File does not exist" << endl;
                    system("PAUSE");
                    exit(1);
                }
                index = 0;
                length = itemNumb.size();
                while (index != length)
                {
                    out_stream << endl;
                    out_stream << itemNumb[index] << endl;
                    out_stream << itemName[index] << endl;
                    out_stream << itemCost[index] << endl;
                    out_stream << itemQuant[index] << endl;
                    index++;
                }
                out_stream.close();
            }
        }
        while (userChoice != 0);
    }

文件io的编辑行,导致运行时错误

    do
    {
        getline(in_stream, tempstr1);
        itemNumb.push_back(tempstr1);
        getline(in_stream, tempstr2);
        itemNumb.push_back(tempstr2);
        in_stream >> tempdoub;
        itemCost.push_back(tempdoub);
        in_stream >> tempint;
        itemQuant.push_back(tempint);
        index++;
    } while (! in_stream.eof());
    in_stream.close();

【问题讨论】:

    标签: c++ file-io vector getline


    【解决方案1】:

    std::getline 的第二个参数只能是 std::basic_string&lt;...&gt;。要阅读intdouble,您应该像这样使用重载的operator&gt;&gt;

    in_stream >> itemCost[index] >> itemQuant[index];
    

    【讨论】:

    • 我把它改成了你放的,但是现在 if 语句导致程序崩溃。
    • 你能发布完整的代码吗?也许,向量中没有足够的空间?
    • 一切都不会,但过去应该不会对它产生影响。
    • @ZacharyMcCloud,正如我已经说过的,我猜是向量空间不足造成的问题。在第一个if 中,您将数据输入到没有为元素分配空间的向量中。尝试改用push_back/emplace_back。如果没有看到 AddSearchList 函数的实现,也没有输入示例,我无法为您提供更多帮助。
    • 哦,糟糕,我在 Add 函数中使用了 push_back,完全忽略了这一点。现在解决这个问题,谢谢
    猜你喜欢
    • 2013-03-31
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2023-03-06
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多