2015-10-21 21 views
-3

是否有像getlinegetchar这样的功能,即使char为空格,它也会得到下一个字符?所以如果我有a bc,我将不得不四次调用该函数以获得'a',' ','b''c'即使它是空白,我如何得到下一个字符?

+1

也许http://en.cppreference.com/w/cpp/io/manip/skipws –

+0

函数getline用1个调用获得一行,而不是4. getchar是完全不同的东西。 – deviantfan

回答

0

getline(cin,variable)

函数getline需要整条生产线因此对于“一个BC”,你将不得不自己来解析该字符串。

0

也许尝试ifs​​tream的,因为是禁用空格跳跃行为操纵:

单字符与标准库的命名字符输入功能的
stream >> std::noskipws; 
1

阅读,不跳过空白。即这个问题有一个不正确的假设。

在C++级别,您可以使用istream::get;有各种过载。

在C级别,getchargetwchar,加上family

这是一个好主意,在文档中查找事物进行培训。

3

未格式化的输入函数不会跳过空格 - 请参见here。听起来像你想get。从上面链接的完整列表中:

以下标准库函数是UnformattedInputFunctions。

std::getline, except that it does not modify gcount. 
basic_istream::operator>>(basic_streambuf*) 
basic_istream::get 
basic_istream::getline 
basic_istream::ignore 
basic_istream::peek 
basic_istream::read 
basic_istream::readsome 
basic_istream::putback, except that it first clears eofbit 
basic_istream::unget, except that it first clears eofbit 
basic_istream::sync, except that it does not modify gcount 
basic_istream::tellg, except that it does not modify gcount 
basic_istream::seekg, except that it first clears eofbit and does not modify gcount 
std::ws, except that it does not modify gcount 

另一种方法是禁用空白跳过内部格式的输入功能:

#include <iomanip> 
... 
char c; 
std::cin >> std::noskipws >> c; 
+0

×1为列表。尼斯。为什么我没有想到这一点。 –

+0

@ Cheersandhth.-Alf:我不能声称有太多的功劳......看到链接在':: get'页面上,而不是直接搜索它。干杯。 –

相关问题