久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='Zz9Sf'><tr id='Zz9Sf'><dt id='Zz9Sf'><q id='Zz9Sf'><span id='Zz9Sf'><b id='Zz9Sf'><form id='Zz9Sf'><ins id='Zz9Sf'></ins><ul id='Zz9Sf'></ul><sub id='Zz9Sf'></sub></form><legend id='Zz9Sf'></legend><bdo id='Zz9Sf'><pre id='Zz9Sf'><center id='Zz9Sf'></center></pre></bdo></b><th id='Zz9Sf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Zz9Sf'><tfoot id='Zz9Sf'></tfoot><dl id='Zz9Sf'><fieldset id='Zz9Sf'></fieldset></dl></div>
  • <legend id='Zz9Sf'><style id='Zz9Sf'><dir id='Zz9Sf'><q id='Zz9Sf'></q></dir></style></legend>
    <tfoot id='Zz9Sf'></tfoot>
      <bdo id='Zz9Sf'></bdo><ul id='Zz9Sf'></ul>

    1. <small id='Zz9Sf'></small><noframes id='Zz9Sf'>

      1. 使用字符串類輸入空格時(shí)出現(xiàn) cin 問題

        Issue with cin when spaces are inputted, using string class(使用字符串類輸入空格時(shí)出現(xiàn) cin 問題)

      2. <tfoot id='ybZgH'></tfoot>
          <bdo id='ybZgH'></bdo><ul id='ybZgH'></ul>
        • <i id='ybZgH'><tr id='ybZgH'><dt id='ybZgH'><q id='ybZgH'><span id='ybZgH'><b id='ybZgH'><form id='ybZgH'><ins id='ybZgH'></ins><ul id='ybZgH'></ul><sub id='ybZgH'></sub></form><legend id='ybZgH'></legend><bdo id='ybZgH'><pre id='ybZgH'><center id='ybZgH'></center></pre></bdo></b><th id='ybZgH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ybZgH'><tfoot id='ybZgH'></tfoot><dl id='ybZgH'><fieldset id='ybZgH'></fieldset></dl></div>
          1. <small id='ybZgH'></small><noframes id='ybZgH'>

                <legend id='ybZgH'><style id='ybZgH'><dir id='ybZgH'><q id='ybZgH'></q></dir></style></legend>

                  <tbody id='ybZgH'></tbody>
                  本文介紹了使用字符串類輸入空格時(shí)出現(xiàn) cin 問題的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有以下代碼:

                  #include <iostream>
                  #include <string>
                  
                  using namespace std;
                  
                  string name;
                  string age;
                  
                  int main() {
                      cout <<"Name: ";
                      cin >> name;
                      cout << endl;
                      cout <<"Age: ";
                      cin >> age;
                      cout << endl;
                      cout << "Your name is " << name << ", and you are " << age << " years old."  << endl;
                      cout << "Press enter to close this application" << endl;
                      getchar();
                      return 0;
                  }
                  

                  我注意到,如果我在名稱的輸入中輸入一個(gè)空格,它不會(huì)給我輸入名稱的機(jī)會(huì),它會(huì)將空格后面的條目視為年齡.如果這是一個(gè)新手錯(cuò)誤,我深表歉意,它可能是.我之前編寫了 Java 并決定改用 C++,因?yàn)樗m合我的需求.我的代碼格式也可能與您的標(biāo)準(zhǔn)很奇怪,如果您愿意,請(qǐng)更正.

                  I noticed that if I put a space in my input for name that it won't give me a chance to input name, and it will view the entry after the space as age. I apologize if this is a newbie mistake, which it probably is. I previously programmed Java and decided I wanted to switch to C++ because it better suits my needs. I also probably format my code weird to your standards, please correct it if you wish to.

                  我還注意到另一個(gè)錯(cuò)誤,我在 Java 中從未真正遇到過任何問題.我不知道如何防止它在完成處理后立即關(guān)閉.我聽說你可以使用system.(pause");但我也被告知不要使用它.我真的很困惑要使用什么.我聽說過使用 getchar();,但它似乎沒有任何作用.

                  I've also noticed another error, something I never really had any problems with in Java. I can't figure out how to prevent it from instantly closing down when it finishes processing. I've heard you can use "system.("pause"); but I've also been told to not use it. I'm really confused on what to use. I've heard to use getchar();, but it doesn't seem to do anything.

                  任何幫助將不勝感激,因?yàn)樵?C++ 方面我是一個(gè)完整的初學(xué)者.

                  Any help would be greatly appreciated, as I'm a complete beginner when it comes to C++.

                  推薦答案

                  以下是運(yùn)行程序時(shí)輸入緩沖區(qū)發(fā)生的情況:

                  Here's what's happening with the input buffer when you run your program:

                  std::cin >> name;
                  

                  您正在等待輸入.當(dāng)您輸入Ryan Cleary"并按回車鍵時(shí),輸入緩沖區(qū)包含:

                  You're waiting for input. When you enter "Ryan Cleary", and press enter, the input buffer contains:

                  Ryan Cleary
                  
                  

                  現(xiàn)在你的 cin 像往常一樣讀取輸入,在空格處停止,像這樣離開你的緩沖區(qū):

                  Now your cin reads input as normal, stopping at whitespace, leaving your buffer like this:

                   Cleary
                  
                  

                  注意開頭的空格,因?yàn)樗陂喿x Ryan 后停止.您的第一個(gè)變量現(xiàn)在包含 Ryan.但是,如果您想要全名,請(qǐng)使用 std::getline.它將一直讀到換行符,而不僅僅是空格.無論如何,繼續(xù):

                  Note the beginning space, as it stops after reading Ryan. Your first variable now contains Ryan. If, however, you want the full name, use std::getline. It will read until a newline, not just whitespace. Anyway, continuing on:

                  std::cin >> age;
                  

                  現(xiàn)在你得到另一個(gè)輸入.不過,那里已經(jīng)有東西了.它跳過空白直到它可以開始讀取,只留下緩沖區(qū):

                  Now you're getting another input. There's already something there, though. It skips the whitespace until it can start reading, leaving the buffer with just:

                  
                  
                  

                  您的第二個(gè)變量獲取文本 Cleary.注意換行符仍在緩沖區(qū)中,這讓我進(jìn)入了第二部分.以始終有效的方式替換 system ("pause"); 很棘手.你最好的選擇通常是接受一個(gè)不太完美的解決方案,或者像我喜歡的那樣,一個(gè)不能保證完全按照它所說的去做:

                  Your second variable gets the text Cleary. Note the newline still in the buffer, which brings me to the second part. Replacing system ("pause"); in a way that always works is tricky. Your best bet is usually to live with a less-than-perfect solution, or as I like to do, one that isn't guaranteed to do exactly what it says:

                  std::cin.get(); //this consumes the left over newline and exits without waiting
                  

                  好的,那么 cin.get() 沒有用.這個(gè)怎么樣:

                  Okay, so cin.get() didn't work. How about this:

                  std::cin.get(); //consume left over newline
                  std::cin.get(); //wait
                  

                  這很好用,但是如果你將它復(fù)制粘貼到?jīng)]有換行符的地方怎么辦?你必須按兩次回車!

                  That works perfectly, but what if you copy-paste it somewhere where the newline isn't left over? You'll have to hit enter twice!

                  解決方案是清除換行符(和其他任何東西),然后等待.這就是 cin.sync() 的目的一>.但是,如注釋部分所示,不能保證像它所說的那樣清除緩沖區(qū),因此如果您的編譯器選擇不清除,則無法使用它.然而,對(duì)我來說,它正是這樣做的,留下了一個(gè)解決方案:

                  The solution is to clear the newline (and anything else) out, and then wait. This is the purpose of cin.sync(). However, as seen in the notes section, it is not guaranteed to clear the buffer out like it says, so if your compiler chooses not to, it can't be used. For me, however, it does exactly that, leaving a solution of:

                  std::cin.sync(); //clear buffer
                  std::cin.get(); //wait
                  

                  system("pause"); 的主要壞處是你不知道它會(huì)在別人的電腦上運(yùn)行什么程序.他們可以更改 pause.exe 或?qū)⒄业降姆旁诘谝晃唬鸁o從得知.這可能會(huì)破壞他們的計(jì)算機(jī),因?yàn)樗赡苁?em>任何程序.

                  The main bad thing about system("pause"); is that you have no idea what program it will run on someone else's computer. They could've changed pause.exe or put one that's found first, and you have no way of knowing. This could potentially ruin their computer due to it being possibly any program.

                  這篇關(guān)于使用字符串類輸入空格時(shí)出現(xiàn) cin 問題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                  The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡(jiǎn)單方法?)
                  Reading from .txt file into two dimensional array in c++(從 .txt 文件讀取到 C++ 中的二維數(shù)組)
                  How to simulate a key press in C++(如何在 C++ 中模擬按鍵按下)
                  Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(為什么在 cin.ignore() 之后沒有 getline(cin, var) 讀取字符串的第一個(gè)字符?)
                  What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)
                • <tfoot id='AOjrH'></tfoot>

                        <i id='AOjrH'><tr id='AOjrH'><dt id='AOjrH'><q id='AOjrH'><span id='AOjrH'><b id='AOjrH'><form id='AOjrH'><ins id='AOjrH'></ins><ul id='AOjrH'></ul><sub id='AOjrH'></sub></form><legend id='AOjrH'></legend><bdo id='AOjrH'><pre id='AOjrH'><center id='AOjrH'></center></pre></bdo></b><th id='AOjrH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='AOjrH'><tfoot id='AOjrH'></tfoot><dl id='AOjrH'><fieldset id='AOjrH'></fieldset></dl></div>
                        1. <legend id='AOjrH'><style id='AOjrH'><dir id='AOjrH'><q id='AOjrH'></q></dir></style></legend>

                          <small id='AOjrH'></small><noframes id='AOjrH'>

                          • <bdo id='AOjrH'></bdo><ul id='AOjrH'></ul>

                              <tbody id='AOjrH'></tbody>
                            主站蜘蛛池模板: 日韩精品免费看 | 毛片资源 | 国产成人在线视频 | 黄色三级视频网站 | 国产精品久久一区二区三区 | 中文字幕日韩一区 | 国产成人精品av在线观 | www操 | 欧美精品99| 三级av网站 | 国产黄色一区 | 一级黄色录像带 | 欧美一区二区在线观看 | 欧美一区二区三区在线视频 | 青青草视频免费在线观看 | 国产亚洲欧美在线 | 天天做天天干 | 精品国产精品三级精品av网址 | 91综合在线 | 成人高清免费 | 色一情一乱一乱一区91av | 亚洲色网址 | 国产无遮挡又黄又爽免费网站 | 免费中文字幕 | 少妇一级淫片aaaaaa | 91av在线播放 | 男人操女人的网站 | 亚洲综合五月 | 免费成年人视频 | 国产色网站 | 欧美一级特黄视频 | 国产日韩在线播放 | 国产白丝精品91爽爽久久 | 精品视频一区二区三区 | 国产毛片毛片毛片 | 欧美一级淫片 | 一区二区三区免费 | 91欧美激情一区二区三区成人 | aaaaaa毛片 | 国产色网站 | 久久av资源 |