久久久久久久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. 使用字符串類輸入空格時出現 cin 問題

        Issue with cin when spaces are inputted, using string class(使用字符串類輸入空格時出現 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>
                  本文介紹了使用字符串類輸入空格時出現 cin 問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有以下代碼:

                  #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;
                  }
                  

                  我注意到,如果我在名稱的輸入中輸入一個空格,它不會給我輸入名稱的機會,它會將空格后面的條目視為年齡.如果這是一個新手錯誤,我深表歉意,它可能是.我之前編寫了 Java 并決定改用 C++,因為它更適合我的需求.我的代碼格式也可能與您的標準很奇怪,如果您愿意,請更正.

                  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.

                  我還注意到另一個錯誤,我在 Java 中從未真正遇到過任何問題.我不知道如何防止它在完成處理后立即關閉.我聽說你可以使用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.

                  任何幫助將不勝感激,因為在 C++ 方面我是一個完整的初學者.

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

                  推薦答案

                  以下是運行程序時輸入緩沖區發生的情況:

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

                  std::cin >> name;
                  

                  您正在等待輸入.當您輸入Ryan Cleary"并按回車鍵時,輸入緩沖區包含:

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

                  Ryan Cleary
                  
                  

                  現在你的 cin 像往常一樣讀取輸入,在空格處停止,像這樣離開你的緩沖區:

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

                   Cleary
                  
                  

                  注意開頭的空格,因為它在閱讀 Ryan 后停止.您的第一個變量現在包含 Ryan.但是,如果您想要全名,請使用 std::getline.它將一直讀到換行符,而不僅僅是空格.無論如何,繼續:

                  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;
                  

                  現在你得到另一個輸入.不過,那里已經有東西了.它跳過空白直到它可以開始讀取,只留下緩沖區:

                  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:

                  
                  
                  

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

                  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() 沒有用.這個怎么樣:

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

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

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

                  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() 的目的一>.但是,如注釋部分所示,不能保證像它所說的那樣清除緩沖區,因此如果您的編譯器選擇不清除,則無法使用它.然而,對我來說,它正是這樣做的,留下了一個解決方案:

                  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"); 的主要壞處是你不知道它會在別人的電腦上運行什么程序.他們可以更改 pause.exe 或將找到的放在第一位,而您無從得知.這可能會破壞他們的計算機,因為它可能是任何程序.

                  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.

                  這篇關于使用字符串類輸入空格時出現 cin 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                  The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡單方法?)
                  Reading from .txt file into two dimensional array in c++(從 .txt 文件讀取到 C++ 中的二維數組)
                  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) 讀取字符串的第一個字符?)
                  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久久久久久久 | 成人午夜电影在线观看 | 免费观看一级毛片视频 | 久久九九99 | 国产1区2区在线观看 | 国产精品不卡一区 | 久久午夜国产精品www忘忧草 | 一级免费视频 | 一级黄色淫片 | 能看的av | 精品视频免费在线 | 日韩一区二区久久 | 亚洲欧美在线视频 | 精品日韩一区 | 亚洲日韩欧美一区二区在线 | 国产视频一区在线 | 国产成人精品免费视频大全最热 | 中文字幕视频在线观看 | 精品欧美一区二区在线观看欧美熟 | 国产在线麻豆精品入口 | 亚洲福利一区 | 久久久精品国产 | 欧美一级精品片在线看 | 亚洲毛片在线 | 久久成人精品 | 一起操网站 | 国产精品久久久久久久久久不蜜臀 | 成人av网站在线观看 | 国产高清在线精品 | 99国产精品久久久 | 欧美日韩精品一区 | 国产精品免费一区二区三区四区 | 国产精品电影网 | 欧美久久久久久 | 欧美中国少妇xxx性高请视频 | 精品久久久久久 | 精品粉嫩aⅴ一区二区三区四区 | 久久久国产一区二区三区 | 伊人免费在线观看高清 | a网站在线观看 | 久久99深爱久久99精品 |