pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="z1t7zxx" 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="55dpxfb" 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="tbvtld5" 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>
                            主站蜘蛛池模板: 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 底部填充胶_电子封装胶_芯片封装胶_芯片底部填充胶厂家-东莞汉思新材料 | 恒温水槽与水浴锅-上海熙浩实业有限公司 | 瑞典Blueair空气净化器租赁服务中心-专注新装修办公室除醛去异味服务! | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | Q361F全焊接球阀,200X减压稳压阀,ZJHP气动单座调节阀-上海戎钛 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 广西资质代办_建筑资质代办_南宁资质代办理_新办、增项、升级-正明集团 | 宽带办理,电信宽带,移动宽带,联通宽带,电信宽带办理,移动宽带办理,联通宽带办理 | 软膜天花_软膜灯箱_首选乐创品牌_一站式天花软膜材料供应商! | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | bng防爆挠性连接管-定做金属防爆挠性管-依客思防爆科技 | 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 钢板仓,大型钢板仓,钢板库,大型钢板库,粉煤灰钢板仓,螺旋钢板仓,螺旋卷板仓,骨料钢板仓 | 团建-拓展-拓展培训-拓展训练-户外拓展训练基地[无锡劲途] | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | LCD3D打印机|教育|桌面|光固化|FDM3D打印机|3D打印设备-广州造维科技有限公司 | 塑料脸盆批发,塑料盆生产厂家,临沂塑料广告盆,临沂家用塑料盆-临沂市永顺塑业 | 武汉高低温试验机-现货恒温恒湿试验箱-高低温湿热交变箱价格-湖北高天试验设备 | 无锡装修装潢公司,口碑好的装饰装修公司-无锡索美装饰设计工程有限公司 | 新密高铝耐火砖,轻质保温砖价格,浇注料厂家直销-郑州荣盛窑炉耐火材料有限公司 | 快速门厂家批发_PVC快速卷帘门_高速门_高速卷帘门-广州万盛门业 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 钢制拖链生产厂家-全封闭钢制拖链-能源钢铝拖链-工程塑料拖链-河北汉洋机械制造有限公司 | 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 高中学习网-高考生信息学习必备平台 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | 低温等离子清洗机(双气路进口)-嘉润万丰 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 胃口福饺子加盟官网_新鲜现包饺子云吞加盟 - 【胃口福唯一官网】 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 |