<bdo id='ybBxY'></bdo><ul id='ybBxY'></ul>

      <tfoot id='ybBxY'></tfoot>

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

        <i id='ybBxY'><tr id='ybBxY'><dt id='ybBxY'><q id='ybBxY'><span id='ybBxY'><b id='ybBxY'><form id='ybBxY'><ins id='ybBxY'></ins><ul id='ybBxY'></ul><sub id='ybBxY'></sub></form><legend id='ybBxY'></legend><bdo id='ybBxY'><pre id='ybBxY'><center id='ybBxY'></center></pre></bdo></b><th id='ybBxY'></th></span></q></dt></tr></i><div id='ybBxY'><tfoot id='ybBxY'></tfoot><dl id='ybBxY'><fieldset id='ybBxY'></fieldset></dl></div>
        <legend id='ybBxY'><style id='ybBxY'><dir id='ybBxY'><q id='ybBxY'></q></dir></style></legend>
      1. getline 无法正常工作?可能是什么原因?

        时间:2023-06-03
        <legend id='Tfwrm'><style id='Tfwrm'><dir id='Tfwrm'><q id='Tfwrm'></q></dir></style></legend>

        • <tfoot id='Tfwrm'></tfoot>

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

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

                <bdo id='Tfwrm'></bdo><ul id='Tfwrm'></ul>

                    <tbody id='Tfwrm'></tbody>
                • 本文介绍了getline 无法正常工作?可能是什么原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  <块引用>

                  可能的重复:
                  getline 不要求输入?

                  在我的程序中发生了一些独特的事情.下面是一些命令集:

                   cout <<"请输入学生全名:";//名称getline( cin , fullName );cout<<"
                  年龄:";//cin年龄年龄;cin >>年龄 ;cout<<"
                  父亲姓名:";//cin 父亲的名字getline(cin,fatherName);cout<<"
                  永久地址:";//cin 永久地址getline(cin,permanentAddress);

                  当我尝试与整个代码一起运行此代码段时.输出程序的工作原理如下:

                  输出:

                  输入学生全名:年龄:20父亲姓名:永久地址:xyz

                  如果你注意到,程序并没有问我全名,而是直接问我年龄.然后它也跳过了父亲的名字并询问了永久地址.这可能是什么原因?

                  我很难把整个代码贴出来,因为它太大了.

                  解决方案

                  由于您还没有发布任何代码.我来猜一猜.

                  使用 getlinecin 时的一个常见问题是 getline 不会忽略前导空白字符.

                  如果在 cin >> 之后使用 getline,getline() 会将此换行符视为前导空格,并且它只会停止进一步读取.>

                  如何解决?

                  在调用getline()

                  之前先调用cin.ignore()

                  或者

                  进行虚拟调用 getline() 以使用 cin >> 中的尾随换行符

                  Possible Duplicate:
                  getline not asking for input?

                  There is some unique thing happening in my program. Here are some set of commands :

                   cout << "Enter the full name of student: ";  // cin name
                   getline( cin , fullName );
                  
                   cout << "
                  Age: ";  // cin age
                   int age;
                   cin >> age ;
                  
                  cout << "
                  Father's Name: ";  // cin father name
                  getline( cin , fatherName );
                  
                  cout << "
                  Permanent Address: ";  // cin permanent address
                  getline( cin , permanentAddress );
                  

                  When i try to run this snippet along with the whole code.The output program works like :

                  output:

                  Enter the full name of student:
                  Age: 20
                  
                  Father's Name:
                  Permanent Address: xyz
                  

                  If you notice ,the program didn't ask me the full name and went on directly to ask me the age.Then it skips the father's name also and asks the permanent address. What could be the reason for this ?

                  It is difficult for me to post the whole code because it is too large.

                  解决方案

                  Since you have not posted any code. I am going to take a guess.

                  A common problem while using getline with cin is getline does not ignore leading whitespace characters.

                  If getline is used after cin >>, the getline() sees this newline character as leading whitespace, and it just stops reading any further.

                  How to resolve it?

                  Call cin.ignore() before calling getline()

                  Or

                  make a dummy call getline() to consume the trailing newline character from the cin >>

                  这篇关于getline 无法正常工作?可能是什么原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用#pragma warning push/pop 是暂时改变警告级别的正确方法吗? 下一篇:为什么宏需要双层间接?

                  相关文章

                      <bdo id='Ep4V4'></bdo><ul id='Ep4V4'></ul>
                    <legend id='Ep4V4'><style id='Ep4V4'><dir id='Ep4V4'><q id='Ep4V4'></q></dir></style></legend>

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

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