Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ getline(cin,name)会导致似乎永无止境的输入提示_C++_Visual Studio 2012 - Fatal编程技术网

C++ getline(cin,name)会导致似乎永无止境的输入提示

C++ getline(cin,name)会导致似乎永无止境的输入提示,c++,visual-studio-2012,C++,Visual Studio 2012,我正在编写一个程序,需要将用户输入读入字符串对象。我使用的是getline(cin,name),但当它出现提示时,我可以输入任何内容并按enter键,但它只会转到下一行,光标会一直闪烁,提示输入更多内容。本质上,输入提示似乎永远不会结束,无论我键入多少个字符或按多少次enter键。我甚至不知道它是否真的在向string对象发送输入。这可能是什么原因造成的 这是到目前为止主要功能的全部内容(尚未完成,开关案例最终将有6个选项,但编译时不会出错)。相关部分从开关案例1开始: #include <

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

这是到目前为止主要功能的全部内容(尚未完成,开关案例最终将有6个选项,但编译时不会出错)。相关部分从开关案例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()
#包括
#包括
#包括
#包括“stdlib.h”
#包括“Bank.h”//我自己的班级。还有一个Bank.cpp,但除非它们被认为是相关的,否则我不会在其中包含代码
使用名称空间std;
void displayAccountInfo()//将检索有关银行对象的信息
void main()
{
int accsCreated=0;//跟踪到目前为止已创建的帐户数量。允许在空数组索引中放置指向新银行对象的指针。
int option=0;//存储用户选择的选项。用于开关情况。当==6时,也结束do while循环。
Bank*accounts[10];//将按顺序保存指向用户创建的每个Bank对象的指针。创建的帐户不超过10个。
Bank*accpntr;//将指向其中一个Bank对象。用于初始化accounts[]数组中指向该对象的指针。
//开始菜单提示
做
{

cout第一个问题是:

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

通过交替提示和输入(在输入中包含行结构),您应该坚持以行为导向的输入,并将所有内容作为行读取,并在适当时使用从字符串转换数值的操作


这应该比使用行分隔符更可靠。

可能重复的Nah,它不是重复的。这个问题首先与需要清除缓冲区有关。我记得包括cin.ignore,但忘记在其中使用“\n”分隔符。也许这个问题会帮助下一个犯我愚蠢错误的人.facepalm哎呀真不敢相信我在这上面呆了这么久。这把它修好了。非常感谢。
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');