2015-10-21 51 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'頁面上,而不是直接搜索它。乾杯。 –

相關問題