复活的cin [英] resurrecting cin

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

问题描述

大家好,

我刚刚发现(对我自己)该程序:


// ********* *****************************

#include< iostream>

int main()

{

int a = 0;

while(true)

{

std :: cout<< 输入一个数字!!! \ n << std :: endl;

std :: cin>> a; ///这里我输入a例如char

if(!std :: cin)

{

std :: cout<<" Oouch,你还没有收听我......我快死了...... \ n" ;;

std :: cin.clear(); ///我想再转一圈而不是

周期永恒

}

std :: cout<< a<< ;" \ n";

}

返回0;

}

// *** ***************************************


永远循环如果我输入一些字符而不是数字。快看一下

书籍没有向我展示任何解决方案,而是输入一些

字符串然后解析。所有我想要的只是复活cin来制作

另一个回合,是否可能?


-

float_dublin

解决方案

float_dublin写道:

大家好,
我刚刚发现(对我自己)该程序:

// ************************************** #include< iostream>



int a = 0;
while(true)
{
std :: cout<< 输入一个数字!!! \ n << std :: endl;
std :: cin>> a; ///这里我输入a例如char
if(!std :: cin)
{
std :: cout<<" Oouch,你没有听我说......我快死了...... 。\ n";
std :: cin.clear(); ///我想再转一圈而不是永久循环
//你需要做同步

std :: cin.sync(); }
std :: cout<< a<<" \ n" ;;
}
返回0;
}
// **** **************************************如果我输入一些字符,我将永远循环数字。快速浏览书籍没有向我展示任何解决方案,而是输入一些
字符串然后解析。我想要的只是复活cin来再次转弯,是否可能?

-
float_dublin


float_dublin写道:


大家好,
我刚刚发现(对我自己)该程序:

// * *************************************
#include< iostream>

int main()
{a / 0;
while(true)
{
std :: cout<< 输入一个数字!!! \ n << std :: endl;
std :: cin>> a; ///这里我输入a例如char
if(!std :: cin)
{
std :: cout<<" Oouch,你没有听我说......我快死了...... 。\ n";
std :: cin.clear(); ///我想再转一圈而不是永恒的周期
}
std :: cout<< a<<" \\\
" ;;
}
返回0;
}
// *******************************如果我输入一些字符而不是数字,*******

将永远循环。快速浏览书籍没有向我展示任何解决方案,而是输入一些
字符串然后解析。我想要的只是复活cin来再次转弯,是否可能?




是的,它是。

如果你输入例如。字符a,然后cin>> a认识到它不能使用这个字符来构造一个数字。转换停止

并且由于没有输入用于形成数字的可用字符输入

操作会做两件事:它会因错误而停止转换。它将

返回该输入流的字符以便稍后处理(可能是另一个>>操作的
)。因此,当循环在下一次翻转时,

操作会一次又一次地看到同​​一个字符。


- >你需要摆脱那种输入。查找流成员函数ignore()。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


Anand写道:

< blockquote class =post_quotes> if(!std :: cin)
{
std :: cout<<" Oouch,你没有听我说......我快死了.. .. \ n";
std :: cin.clear(); ///我想再转一圈而不是永久循环


//你需要做同步
std :: cin.sync();




sync()与问题无关。

OP需要摆脱错误的输入。 ignore()这样做。

-

Karl Heinz Buchegger
kb ****** @ gascad.at


Hi All,
I just have found out (for myself) that the program:

//**************************************
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here I input "a" eg char
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I''m dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity
}
std::cout<<a<<"\n";
}
return 0;
}
//**************************************

cycles forever if i input some char instead of numbers. Fast look throw
books have not showed me any solution for this but inputing into some
string and then parsing. All i want is just resurrect cin to make
another turn, is it possible?

--
float_dublin

解决方案

float_dublin wrote:

Hi All,
I just have found out (for myself) that the program:

//**************************************
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here I input "a" eg char
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I''m dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity // you need to do a sync
std::cin.sync(); }
std::cout<<a<<"\n";
}
return 0;
}
//**************************************

cycles forever if i input some char instead of numbers. Fast look throw
books have not showed me any solution for this but inputing into some
string and then parsing. All i want is just resurrect cin to make
another turn, is it possible?

--
float_dublin



float_dublin wrote:


Hi All,
I just have found out (for myself) that the program:

//**************************************
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here I input "a" eg char
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I''m dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity
}
std::cout<<a<<"\n";
}
return 0;
}
//**************************************

cycles forever if i input some char instead of numbers. Fast look throw
books have not showed me any solution for this but inputing into some
string and then parsing. All i want is just resurrect cin to make
another turn, is it possible?



Yes, it is.
If you input eg. the character a, then cin >> a recognizes that it can''t
use this character for constructing a number. This the conversion stops
and since no usable character for forming a number was entered the input
operation does 2 things: it stops the conversion with an error. it feeds
back that character to the input stream to be processed later (probably
by another >> operation). So when the loop turns over the next time, the

operation sees that very same character again and again and again.


-> You need to get rid of that input. Look up stream member function ignore().

--
Karl Heinz Buchegger
kb******@gascad.at


Anand wrote:

if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I''m dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity


// you need to do a sync
std::cin.sync();



sync() has nothing to do with the problem.
The OP needs to get rid of the erronous input. ignore() does this.
--
Karl Heinz Buchegger
kb******@gascad.at


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

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