<i id='FCFIH'><tr id='FCFIH'><dt id='FCFIH'><q id='FCFIH'><span id='FCFIH'><b id='FCFIH'><form id='FCFIH'><ins id='FCFIH'></ins><ul id='FCFIH'></ul><sub id='FCFIH'></sub></form><legend id='FCFIH'></legend><bdo id='FCFIH'><pre id='FCFIH'><center id='FCFIH'></center></pre></bdo></b><th id='FCFIH'></th></span></q></dt></tr></i><div id='FCFIH'><tfoot id='FCFIH'></tfoot><dl id='FCFIH'><fieldset id='FCFIH'></fieldset></dl></div>

  • <tfoot id='FCFIH'></tfoot>
    1. <legend id='FCFIH'><style id='FCFIH'><dir id='FCFIH'><q id='FCFIH'></q></dir></style></legend>
        <bdo id='FCFIH'></bdo><ul id='FCFIH'></ul>

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

        为什么在 cin.ignore() 之后没有 getline(cin, var) 读取

        时间:2023-10-08

        1. <tfoot id='K6rX6'></tfoot>

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

            <bdo id='K6rX6'></bdo><ul id='K6rX6'></ul>
            • <small id='K6rX6'></small><noframes id='K6rX6'>

                    <tbody id='K6rX6'></tbody>

                • <i id='K6rX6'><tr id='K6rX6'><dt id='K6rX6'><q id='K6rX6'><span id='K6rX6'><b id='K6rX6'><form id='K6rX6'><ins id='K6rX6'></ins><ul id='K6rX6'></ul><sub id='K6rX6'></sub></form><legend id='K6rX6'></legend><bdo id='K6rX6'><pre id='K6rX6'><center id='K6rX6'></center></pre></bdo></b><th id='K6rX6'></th></span></q></dt></tr></i><div id='K6rX6'><tfoot id='K6rX6'></tfoot><dl id='K6rX6'><fieldset id='K6rX6'></fieldset></dl></div>
                  本文介绍了为什么在 cin.ignore() 之后没有 getline(cin, var) 读取字符串的第一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在用 C++ 创建一个简单的控制台应用程序,它从用户那里获取字符串和字符输入.为简单起见,我想使用 stringchar 数据类型将输入从 cin 传递到.

                  I'm creating a simple console application in C++ that gets string and char inputs from the user. To make things simple, I would like to use the string and char data types to pass input from cin to.

                  为了获取字符串输入,我使用了 getline 方法:

                  To get string inputs, I'm using the getline method:

                  string var;
                  cin.ignore(); //I used ignore() because it prevents skipping a line after using cin >> var
                  getline(cin, var);
                  

                  为了获得字符输入,我使用了 cin >> var 方法:

                  To get char inputs, I'm using the cin >> var method:

                  char var;
                  cin >> var;
                  

                  这在大多数情况下都可以正常工作.但是,当我使用 getline 输入字符串时,它会忽略字符串的第一个字符.

                  This works fine for the most part. However, when I enter a string using getline, it ignores the first character of my string.

                  是否可以使用 getlinecin >> 而不必使用 ignore,或者我可以调用的方法来确保我的没有跳过第一个字符?

                  Is it possible to use getline and cin >> without having to use ignore, or a method I can call to ensure that my first character isn't skipped?

                  这是我同时使用 getlinecin >> 的完整代码示例:

                  This is a full sample of code where I use both getline and cin >>:

                  string firstName;
                  string lastName;
                  char gender = 'A';
                  
                  cout << "First Name: ";
                  cin.ignore();
                  getline(cin, firstName);
                  
                  
                  cout << "Last Name: ";
                  cin.ignore();
                  getline(cin, lastName);
                  
                  while(genderChar != 'M' && genderChar != 'F')
                  {
                      cout << "Gender (M/F): ";
                      cin >> genderChar;
                      genderChar = toupper(genderChar);
                  }
                  

                  推荐答案

                  cin>>var;

                  只从缓冲区中获取var,而将 留在缓冲区中,然后立即被 getline

                  only grabs the var from the buffer, it leaves the in the buffer, which is then immediately grabbed up by the getline

                  所以,下面就好了,(如果我理解正确的话)

                  So, following is just fine, (if I understood correctly your problem)

                  cin>>var;
                  cin.ignore();     //Skip trailing '
                  '
                  getline(cin, var);
                  

                  根据您的编辑帖子

                  你不必为 geline 使用 cin.ignore();

                  这从缓冲区中提取字符并将它们存储到 firstName 或 (lastName) 直到这里的分隔符 -newline (' ').

                  This extracts characters from buffer and stores them into firstName or (lastName) until the delimitation character here -newline (' ').

                  这篇关于为什么在 cin.ignore() 之后没有 getline(cin, var) 读取字符串的第一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:scanf 格式输入的 cin 类比是什么? 下一篇:如何在 C++ 中模拟按键按下

                  相关文章

                  最新文章

                • <legend id='q2H29'><style id='q2H29'><dir id='q2H29'><q id='q2H29'></q></dir></style></legend>
                • <tfoot id='q2H29'></tfoot>
                    <bdo id='q2H29'></bdo><ul id='q2H29'></ul>

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

                  1. <i id='q2H29'><tr id='q2H29'><dt id='q2H29'><q id='q2H29'><span id='q2H29'><b id='q2H29'><form id='q2H29'><ins id='q2H29'></ins><ul id='q2H29'></ul><sub id='q2H29'></sub></form><legend id='q2H29'></legend><bdo id='q2H29'><pre id='q2H29'><center id='q2H29'></center></pre></bdo></b><th id='q2H29'></th></span></q></dt></tr></i><div id='q2H29'><tfoot id='q2H29'></tfoot><dl id='q2H29'><fieldset id='q2H29'></fieldset></dl></div>