getline()函数正在跳过输入

时间:2013-01-25 16:10:31

标签: c++ visual-studio-2010 io

  

可能重复:
  cin and getline skipping input

请查看以下代码

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函数会跳过第一个输入。它不显示输入的光标,而只是它来到第二个输入!它显示在图像中。请帮助enter image description here

0 个答案:

没有答案