gpt4 book ai didi

c++ - getline() 和 vector (C++) 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:45 25 4
gpt4 key购买 nike

我正在尝试从文本文件中获取输入并使用 getline() 将数据放入四个 vector 中。该文件包含两个字符串、一个 double 字符串和一个整数,它们都位于不同的行中,每组由一个空行分隔。

    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 是字符串 vector ,itemCost 是 double ,itemQuant 是整数。字符串的行没有给出错误,但是 double 和整数的行给出了相同的错误,类型根据它是哪个 vector 而改变。

错误:

no matching function for call to 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();

最佳答案

std::getline 的第二个参数可能只有 std::basic_string<...> .阅读intdouble你应该使用重载 operator>>像这样:

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

关于c++ - getline() 和 vector (C++) 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com