1

我正在尝试从文本文件中获取输入,并使用 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();
4

1 回答 1

1

的第二个参数std::getline可能是 only std::basic_string<...>。要阅读intdouble您应该像这样使用重载operator>>

in_stream >> itemCost[index] >> itemQuant[index];
于 2013-05-17T14:03:03.107 回答