如何从stdin C ++中正确读取和解析整数字符串 [英] How to properly read and parse a string of integers from stdin C++
问题描述
正如标题所说,我正在阅读stdin中的一串整数。我试图读入的数据在文本文件中以下列形式显示:
Just as the title says, I'm reading in a string of integers from stdin. The data i'm trying to read in appears in the following form in a text file:
3
4
7 8 3
7 9 2
8 9 1
0 1 28
etc...
前两行始终只是单个数字(没有问题!),以下各行各有3个数字。此文件被重定向为stdin到我的程序(myprogram< textfile)。
The first two lines are always just single numbers (no problems there!) and the following lines each have 3 numbers. This file is redirected as stdin to my program (myprogram < textfile).
我尝试了很多东西,但还是没能成功做到这一点!它看起来很简单,但我一直绊倒在哪里(或如何)我应该转换为整数。这是我最近的尝试:
I've tried so many things but have not been able to successfully to do this! It seems so simple but I keep tripped up on where (or how) i should convert to an integer. Here is my latest attempt:
int main()
{
string str, str1, str2;
int numCities;
int numRoads, x, y, z;
cin >> numCities >> numRoads;
cout << numCities << " " << numRoads << endl;
//getline(cin, str1);
while( getline(cin, str))
{
char *cstr;
cstr = new char[str.size()+1];
strcpy(cstr, str.c_str());
x = atoi(strtok(cstr, " ")); //these will be stored in an array or something
y = atoi(strtok(NULL, " ")); //but right now i just want to at least properly
z = atoi(strtok(NULL, " ")); //store the appropriate values in these variables!
}
return 0;
}
当我尝试使用atoi时,我发生了段错...
I segfault when i try to use atoi...
提前致谢!
推荐答案
首先,您正在使用 cin
读取您的输入,然后使用 getline
。但是,当您阅读最后一个号码时,会丢失'\ n'
。你的第一个 getline(cin,str)
正在读取'\ n'
,这就是你得到段错误的原因。要检查它,请在代码中添加以下行:
First, you are using cin
to read your input, then you use getline
. However, when you read the last number, there is a '\n'
missing. Your first getline(cin, str)
is reading the '\n'
, this is why you get the segfault. To check it, add the following lines to your code:
// just change these lines:
getline(cin, str);
cout << str << endl;
while(getline(cin, str))
{
cout << str << endl;
// ...
您的输出将是:
// output
3 4
7 8 3
7 9 2
8 9 1
0 1 28
您是否只想得到所有数字as int
?对于此输入,我建议:
Do you only want to get all the numbers as int
? Than, for this input, I recommend:
while (cin >> x >> y >> z)
{
cout << x << " " << y << " " << z;
}
getline()
解决方案
getline()
solution
您仍然可以使用 getline
功能。然后,我建议您更改输入读数,以便在前两行使用 getline
。通过这种方式,您将不会遇到'\ n'
的问题。但是,我觉得它太复杂了,我不建议。特别是如果它是一个马拉松问题。
You can still use getline
function. Then, I suggest you to change your input reading to use getline
for your first two lines too. By doing this way, you will not have problems with '\n'
. But, I think it gets too complicated and I don't recommend. Specially if it is a marathon problem.
// ...
getline(cin, str);
numCities = atoi( str.c_str() );
getline(cin, str);
numRoads = atoi( str.c_str() );
cout << numCities << " " << numRoads << endl;
while(getline(cin, str))
{
char *cstr;
cstr = new char[str.size()+1];
strcpy(cstr, str.c_str());
x = atoi(strtok(cstr, " "));
y = atoi(strtok(NULL, " "));
z = atoi(strtok(NULL, " "));
}
这篇关于如何从stdin C ++中正确读取和解析整数字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!