cin.getline()问题 [英] cin.getline() problem

查看:51
本文介绍了cin.getline()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想从用户那里得到一个整数。


cin> a;



如果用户插入一个字符而不是一个int,那么程序就会变得很疯狂。所以我试过这样的事情:


> int get_value(){

int a;

char b [6];

cout<< 插入值 << endl;

cin.getline(b,7,''\ n'');

a = atoi(b);

cout << a: << a<< endl;

返回0;
}



此程序运行良好。但是如果我把这个代码放在我的主要的
程序中,那么b-array就不会得到一个值。

当我更改代码时,数组没有得到值。 br />


> int get_value(){

int a;

int x;

char b [6];

cout<< 插入值 << endl;

cin> x;

cout<< 插入值 << endl;

cin.getline(b,7,''\ n'');

a = atoi(b);

cout << a: << a<< endl;

返回0;
}



cin>有什么问题?在cin.getline()之前?


最好的问候


马库斯

解决方案

Markus写道:





我想从中得到一个整数用户。


> cin> a;



如果用户插入一个字符而不是一个int,那么程序就会变得很疯狂。



然后你可能忘了在阅读后检查流状态。


所以我尝试了类似的东西这个:


>> int get_value(){
int a;
char b [6];
cout< ;< 插入值 << endl;
cin.getline(b,7,''\ n'');
a = atoi(b);
cout<< a: << a<< endl;
返回0;
}



这个程序效果很好。但是如果我把这个代码放在我的主要的
程序中,那么b-array就不会得到一个值。

当我更改代码时,数组没有得到值。 br />


>> int get_value(){
int a;
int x;
char b [6];
cout<< 插入值 << endl;
cin> x;
cout<< 插入值 << endl;
cin.getline(b,7,''\ n'');
a = atoi(b);
cout<< a: << a<< endl;
返回0;
}



cin>有什么问题?在cin.getline()之前?



输入x的值时,按下后退。结果

\ n留在缓冲区中,并被你的getline电话接听。


Markus写道:





我想从用户那里得到一个整数。


> cin> a;



如果用户插入一个字符而不是一个int,那么程序就会变得很疯狂。



[snip]


您应该测试成功:


if (cin> a){

//输入可以解析为int。

}否则{

//输入不是INT。采取错误恢复措施。

}


最佳


Kai-Uwe Bux


2006年9月1日02:42:54 -0700 in comp.lang.c ++,Markus

< de ********* **** @ web.dewrote,


> cin>的问题是什么?在cin.getline()之前?



它在流中留下''\ n'',getline将其视为一个空行。


Hi,

I want to get an integer from the user.

cin >a;

If the user inserts a character instead of an int the program goes
crazy. So I tried something like this:

>int get_value() {
int a;
char b[6];
cout << "Insert a value " << endl;
cin.getline(b, 7, ''\n'');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}

This program works pretty well. But if I put this code in my main
program the b-array does not get a value.
When I change my code the array does not get a value.

>int get_value() {
int a;
int x;
char b[6];
cout << "Insert a value" << endl;
cin >x;
cout << "Insert a value " << endl;
cin.getline(b, 7, ''\n'');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}

What is the problem with the cin > before the cin.getline() ?

Best Regards

Markus

解决方案

Markus wrote:

Hi,

I want to get an integer from the user.

>cin >a;

If the user inserts a character instead of an int the program goes
crazy.

Then you probably forgot to check for the stream state after reading.

So I tried something like this:

>>int get_value() {
int a;
char b[6];
cout << "Insert a value " << endl;
cin.getline(b, 7, ''\n'');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}


This program works pretty well. But if I put this code in my main
program the b-array does not get a value.
When I change my code the array does not get a value.

>>int get_value() {
int a;
int x;
char b[6];
cout << "Insert a value" << endl;
cin >x;
cout << "Insert a value " << endl;
cin.getline(b, 7, ''\n'');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}


What is the problem with the cin > before the cin.getline() ?

When you type in the value for x, you press return aftewards. The resulting
\n stays in the buffer and gets picked up by your getline call.


Markus wrote:

Hi,

I want to get an integer from the user.

>cin >a;

If the user inserts a character instead of an int the program goes
crazy.

[snip]

You should test for success:

if ( cin >a ) {
// input could be parsed as an int.
} else {
// input was not an int. take error recovery measures.
}

Best

Kai-Uwe Bux


On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,

>What is the problem with the cin > before the cin.getline() ?

It leaves ''\n'' in the stream, and getline sees it as an empty line.


这篇关于cin.getline()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆