仅通过istream函数从输入流中删除空格 [英] Remove whitespace from input stream by only means of istream functions
本文介绍了仅通过istream函数从输入流中删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入小数之后,是否有任何方法可以删除结尾的空格?
例如:
10 A
我想捕获空格结束后的第一个字符。
我尝试到目前为止:
cout< <请输入数字:;
cin>> n;
if(cin.peek ='')
//捕获空格的东西
if(cin.fail()|| cin.peek()!='\\\
')
cout <<不是数字。<<<< endl;
else
cout< / code>
可以通过istream中的函数执行此操作?
知道cin.fail可以做好事,但仍然不认为输入的10A是失败)
解决方案谢谢
可以通过这个简单的方法来完成:
cout<<请输入数字:;
cin>> n;
while(cin.peek()=='')
cin.ignore(1,'');
if(cin.fail !='\\\
')
cout<< 不是数字。 << endl;
else
cout<< 一个号码。 << endl;
Is there any way of to remove the trailing whitespace after entered a decimal?
E.g.:
10 A
I want to catch the first character after the whitespace ends. (Which gotta be \n to be true. if not, then false
My attempt so far:
cout << "Please enter a number: ";
cin >> n;
if (cin.peek() == ' ')
//Something to catch the whitespaces
if(cin.fail() || cin.peek() != '\n')
cout << "Not a number." << endl;
else
cout << "A number." << endl;
Possible to do that by functions in istream?
(I know cin.fail can do good deeds, but it still doesn't consider an input of 10A as a fail)
解决方案 Thanks for your help.
With strings it do be easy yes. Though not allowed to use it.
Can be done by this simple method:
cout << "Please enter a number: ";
cin >> n;
while (cin.peek() == ' ')
cin.ignore(1,' ');
if(cin.fail() || cin.peek() != '\n')
cout << "Not a number." << endl;
else
cout << "A number." << endl;
这篇关于仅通过istream函数从输入流中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文