【问题标题】:getline(cin, name) causes seemingly never-ending prompt for inputgetline(cin, name) 导致看似永无止境的输入提示
【发布时间】:2014-05-18 10:19:35
【问题描述】:

我正在编写一个程序,该程序需要将用户输入读入字符串对象。我正在使用 getline(cin, name),但是当它到达提示符时,我可以输入任何内容并按 Enter,但它只会转到下一行并且光标会一直闪烁,提示输入更多内容。本质上,输入提示似乎永远不会结束,无论我输入多少字符或按回车多少次。我什至不知道它是否真的将输入发送到字符串对象中。这可能是什么原因造成的?

这是到目前为止的全部主要功能(它不完整,switch-case最终会有6个选项,但它编译没有错误)相关部分从switch case 1开始:

#include <string>
#include <array>
#include <iostream>
#include "stdlib.h"
#include "Bank.h"   //My own class. There is also a Bank.cpp, but I won't include the code in these unless they're deemed relevant

using namespace std;

void displayAccountInfo(); //will retrieve info on bank object

void main()
{
   int accsCreated = 0; //Keeps track of how many accounts have been created so far. Allows placement of pointer to new bank object at an empty array index.
   int option = 0;      //Stores the option chosen by the user. Used in switch-case. Also ends do-while loop when ==6.
   Bank* accounts[10];  //Will hold pointers to each bank object created by user in sequential order. No more than 10 accounts will ever be created.
   Bank* accpntr;       //Will point to one of the Bank objects. Used to initialize a pointer to the object in the accounts[] array.

   //begin Menu prompt
   do
   {
      cout << "[1] Create Bank object with values for accountNumber, name, and balance." << endl;
      cout << "[2] Create Bank object with no parameters." << endl;
      cout << "[6] End program" << endl;
      cin >> option;

      //begin option branch:
      switch (option)
      {
      case 1:
         {
            int num;             //holds account number
            string name;         //will hold account name as string object for use in Bank constructor
            double balance;      //holds account balance

            cout << endl << "Enter an account number: ";
               cin >> num;
            cout << endl << "Enter a name for the account: ";
               cin.ignore(std::numeric_limits<std::streamsize>::max()); //clears cin's buffer so getline() does not get skipped
               getline(cin,name);
            cout << endl << "Enter the balance of the account: ";
               cin >> balance;
            cout << endl;

            accpntr = new Bank(num, name, balance);   //creates a new bank object with attributes and a reference to it
            accounts[accsCreated] = accpntr; //adds pointer to new bank object to first empty spot in array
            accsCreated++;   //increments total of how many bank objects have been created/what index position of accounts[] to create the next one at
         }  break;   //end case 1
      case 2:
         {
            accpntr = new Bank();   //creates a new bank object with an accountNumber of 9999, an empty name attribute, and a balance value of zero.
            accounts[accsCreated] = accpntr; //adds pointer to new bank object to first empty spot in array
            accsCreated++;   //increments total of how many bank objects have been created/what index position of accounts[] to create the next one at
         }  break;   //end case 2

      case 6:
         {
            cout << "Ending Program." << endl;
         }
      }  //end switch-case block
   }
   while (option != 6); //end menu prompt when user chooses option 6.
      //end menu block
}  //end function main()

【问题讨论】:

  • cin and getline skipping input 的可能重复项
  • 不,它不是重复的。这个问题与首先需要清除缓冲区有关。我记得包含 cin.ignore,但忘记在其中使用 '\n' 分隔符。也许这个问题会帮助下一个犯我愚蠢错误的人。

标签: c++ visual-studio-2012


【解决方案1】:

第一个问题在这里:

cin.ignore(std::numeric_limits<std::streamsize>::max());

istream::ignore 的行为类似于其他输入函数。如果缓冲区中没有输入,它将阻塞并等待直到它可用。一旦它忽略了你告诉它的尽可能多的字符,或者一旦它到达分隔符(第二个参数,你没有指定,它默认为traits::eof),它就会停止。你甚至连getline的电话都打不通。

要修复它,请指定分隔符:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

【讨论】:

  • facepalm 哎呀。不敢相信我已经坚持了这么久。这解决了它。非常感谢。
【解决方案2】:

使用交替提示和输入(在您的输入中填充线结构),您应该坚持面向行的输入并将所有内容都作为一条线读取,并在适当的时候使用从字符串转换数值的操作。

这应该比使用行分隔符更健壮。

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 2013-09-23
    • 2021-08-26
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多