簡體   English   中英

C ++使用cin.get獲取字符,如何跳過\\ n並僅捕獲第一個字符?

[英]C++ getting a character using cin.get, how to skip \n and grab only the first character?

我正在做一個學校項目,用戶應在其中輸入開關/案例菜單的內容。 我做了一個單獨的對象要在菜單中讀取。 該程序全部有關生命游戲。 但是,我正在努力如何忽略所有\\ n並仍僅讀取第一個字符。 因此,當用戶輸入\\ n \\ n \\ n RI喜歡返回R時,就像用戶輸入\\ n \\ n $$一樣。 但是現在我的代碼執行了鏈接到R的命令三次。

char wereld::leesoptie ( ) {    
char keuze = cin.get ( );       //So here the user will imput his \n \n RR
if (keuze == '\n') {
    while (keuze == '\n')       //I skip the \n's like this
    keuze = cin.get ( );
}                               
return keuze;                   //The returned value should be only the first real character of the string.
}//leesoptie

接下來是我用來執行leesoptie函數的代碼:

int wereld::parameters (){
-----
keuze = leesoptie ( );
switch (keuze) {
        case 'T': case 't': return 0;
        ---
char c;
std::cin >> c;

默認情況下,這將讀取一個字符,而忽略任何空格(包括換行符)。 查看更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM