可能重复:
cin 和 getline 跳过输入
请看下面的代码
UIHandler.h
#pragma once
class UIHandler
{
public:
~UIHandler(void);
void bookAddWizard();
//static UIHandler *getInstance();
static UIHandler& getInstance();
private:
int bookCounter;
UIHandler();
};
UIHandler.cpp
#include "UIHandler.h"
#include <iostream>
using namespace std;
UIHandler::UIHandler()
{
{
}
}
UIHandler::~UIHandler(void)
{
}
UIHandler& UIHandler::getInstance()
{
static UIHandler uiHandler;
return uiHandler;
}
void UIHandler::bookAddWizard()
{
cout << endl << endl << endl;
cout << "..................Welcome to Book Adding Wizard................." << endl;
while(true)
{
if(bookCounter==10 || bookCounter>10)
{
cout << endl << endl;
cout << "Library is Full. No books will be accepted." << endl;
mainCaller();
break;
}
string id;
string bookName;
string auther;
string publisher;
string confirmation;
cout << endl;
cout << "Please enter Book ID: ";
//cin >> id;
getline(cin,id);
cout << endl;
cout << "Please enter Book Name: ";
//cin >> bookName;
getline(cin,bookName);
cout << "Please enter Auther's Name: ";
//cin >> auther;
getline(cin,auther);
cout << "Please enter publisher: ";
//cin >> publisher;
getline(cin,publisher);
books[bookCounter] = new Book();
books[bookCounter]->setAuther(auther);
books[bookCounter]->setBookName(bookName);
books[bookCounter]->setId(id);
books[bookCounter]->setPublisher(publisher);
books[bookCounter]->createCopy(bookCounter);
cout << "Book Added" << endl;
bookCounter++;
cout << bookCounter << endl;
cout << "Do you want to continue? Y/N: ";
cin >> confirmation;
if(confirmation=="Y" || confirmation=="y")
{
continue;
}
else
{
break;
}
}
}
在这里,由于某种原因,getline 函数跳过了第一个输入。它不显示输入的光标,而是简单地进入第二个输入!它显示在图像中。请帮忙