gpt4 book ai didi

c++ - C++ 文件中逗号后的股票值

转载 作者:行者123 更新时间:2023-11-28 00:23:39 26 4
gpt4 key购买 nike

我正在尝试检测并忽略同一行到股票值(value)的逗号。

我试了getline的3个参数,没用。我也试过迭代器,一样。

我的文本文件包含以下值:

Bob Jackson,25,Sick
Eric Smith,99,Deceased
Alice Vanderlend,16,Healthy

在下面的代码中,我在我的失败尝试中添加了注释...

string str,fn, ln, age, sta;
ifstream input("text.txt");


while (getline(input, str))
{
/*for ( string::iterator it=str.begin(); it!=str.end(); ++it)
{
if ((*it) == ','){continue;};
}*/

//string vir;
istringstream iss(str);

//if(!(getline(input, vir, ','))){break;}

iss >> fn >> ln >> age >> sta;

cout
<< "First Name: " << fn
<< "\nLast Name: " << ln
<< "\nAge: " << age
<< "\nStatus: " << sta << endl << endl;

}

我期望得到的输出是

First Name: Bob
Last Name: Jackson
Age: 25
Status: Sick
...

相反,我得到:

First Name: Bob
Last Name: Jackson,25,Sick
Age:
Status:
...

如何忽略逗号来存储下一个值?

最佳答案

如下声明一个虚拟变量:

char comma;

并读出多余的逗号:

iss >> fn >> ln >> comma>> age >> comma >> sta;

代替

iss >> fn >> ln >> age >> sta;

关于c++ - C++ 文件中逗号后的股票值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172548/

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