C++ 读字符串和双精度
我在读取文件中的字符串时遇到问题,然后从文件中读取一个double。我的教授建议我在每一个输入行后面放一个特殊的getline,但它不起作用,我推断这就是程序的问题所在。有没有一种更简单的方法来参加双打 输入文件的示例如下:C++ 读字符串和双精度,c++,string,double,C++,String,Double,我在读取文件中的字符串时遇到问题,然后从文件中读取一个double。我的教授建议我在每一个输入行后面放一个特殊的getline,但它不起作用,我推断这就是程序的问题所在。有没有一种更简单的方法来参加双打 输入文件的示例如下: John Smith 019283729102380 300.00 2000.00 Andrew Lopez 293481012100121 400.00 1500.00 守则内容如下: while(! infile.eof()) { getline(infile,acc
John Smith
019283729102380
300.00
2000.00
Andrew Lopez
293481012100121
400.00
1500.00
守则内容如下:
while(! infile.eof())
{
getline(infile,accname[count],'\n');
getline(infile, refuse, '\n');
getline(infile,accnum[count],'\n');
getline(infile, refuse, '\n');
infile>>currbal[count];
getline(infile, refuse, '\n');
infile>>credlim[count];
getline(infile, refuse, '\n');
count++;
}
编辑:根据OP的澄清进行更新 根据您提供的示例,这应该可以:
while(1) {
std::string s1;
if(!std::getline(infile, s1))
break;
std::string s2;
if(!std::getline(infile, s2))
break;
double d1, d2;
if(!(infile >> d1 >> d2))
break;
accname[count] = s1;
accnum[count] = s2;
currball[count] = d1;
credlim[count] = d2;
count++;
}
此代码适用于以下输入:
Adam Sandler
0112233
5 100
Ben Stein
989898
100000000
1
Adam Sandler
0112233
5 100
Ben Stein
989898
100000000
1
但它不适用于以下输入:
Adam Sandler
0112233
5 100
Ben Stein
989898
100000000
1
Adam Sandler
0112233
5 100
Ben Stein
989898
100000000
1
我认为Rob Adams的回答在解析多个记录时有一个小错误。第一次提取
d2
后,示例输入流将包含\nBen Stein\n9898\n100000000\n1
,因此对std::getline()
的下一次调用将提取一个空字符串,而不是Ben Stein
。因此,似乎有必要在每次while循环迭代结束时使用一个空字符串
我认为以下代码示例提供了一个修复:
#include <iostream>
#include <sstream>
int main(int argc, char** argv) {
using std::stringstream;
stringstream infile(stringstream::in | stringstream::out);
infile << "John Smith\n";
infile << "019283729102380\n";
infile << "300.00\n";
infile << "2000\n";
infile << "Andrew Lopez\n";
infile << "293481012100121\n";
infile << "400.00\n";
infile << "1500.00\n";
std::string account_name;
std::string account_number;
double current_balance;
double credit_limit;
int count = 0;
while (std::getline(infile, account_name) &&
std::getline(infile, account_number) >> current_balance >>
credit_limit) {
std::cout << account_name << std::endl;
std::cout << account_number << std::endl;
std::cout << current_balance << std::endl;
std::cout << credit_limit << std::endl;
/*
accname[count] = account_name;
accnum[count] = account_number;
currball[count] = current_balance;
credlim[count] = credit_limit;
*/
count++;
// Consume the leading newline character, which seprates the credit limit
// from the next account name, when infile contains multiple records.
//
// For example, after consuming the record for John Smith, the stream will
// contain: "\nAndrew Lopez\n293481012100121\n400.00\n1500.00\n". If you
// don't consume the leading newline character, calling std::getline() to
// parse the next account name will extract an empty string.
std::string blank;
std::getline(infile, blank);
}
return 0;
}
#包括
#包括
int main(int argc,字符**argv){
使用std::stringstream;
stringstream填充(stringstream::in | stringstream::out);
infle什么不起作用?你给了它什么输入,发生了什么?@Oli,它实际上只是冻结了程序,然后我注释掉了“拒绝”和与double有关的infle,它运行(在输出垃圾时)只是部分相关:while(!infle.eof()){/*read data*/}几乎肯定是一个bug。不是bug,而是一个bug。你能告诉我哪本C++书告诉你使用EFF()函数吗?然后我可以出去暗杀作者。如果你不能告诉我你从哪本书中得到的原因是什么?“山姆,是的,那是个bug。<代码>不能预测即将到来的读取将遇到EOF。考虑。在正确读取“3”之后,它停止在最后的换行符上。由于EOF位尚未设置,它试图读取“3”之后的值,而坏事发生。