Cin.get() does not wait for a keypress even with cin.ignore() before it

眉间皱痕 提交于 2019-12-24 17:35:06

问题


I am a somewhat new programmer practicing with this program below. For whatever reason, at the end of my program, cin.ignore() is being completely skipped by the compiler and moves straight onto cin.get(), but there was already a key-press before, so the compiler skips it altogether and finishes the program without waiting for a key-press. I've tried putting cin.get() and cin.ignore() in the switch case statement but the same error occurs. I've searched about this problem around the web and can't find anything that relates to my problem. Here is my code in its entirety:

#include <iostream>
#include <cstdlib>
#include <cstring>


using namespace std;
class mobs 
    {
    public:
        mobs();
        ~mobs();
        void define();
        void dismi();
        void getinfo();
        int stat[2];
        string name;
        string bio;
    protected:  


        int health;
        int level;

    };
    mobs dragon;
    mobs::mobs()
    {

        int stat[2];

    }
    mobs::~mobs()
    {


    }

int selection;

void mobs::dismi()
{
    getinfo();
    cout<<"Level:" <<level<<"Health:" <<health <<endl  <<endl <<endl       <<"Name:" <<name  <<"Bio:" <<bio <<endl <<endl;

}

void mobs::getinfo()
{
    define();

}

void mobs::define()
{
    stat[0] = health;
    stat[1] = level;

}


int main()
{   
    dragon.stat[0] = 100;
    dragon.stat[1] = 13;
    dragon.name = "Ethereal Dragon, Dragon of the plane";
    dragon.bio = "A dragon that can only be found in the ethereal plane.This dragon has traditional abilites such as flight and the ability to breath fire.  The Ethereal Dragon's other known abilites are teleportation or magic.";


    cout<<"Welcome to the Mob Handbook. " <<endl <<endl <<"Please make a selection "<<endl;
    cout<<"1.Ethereal Dragon" <<endl<<"2." <<endl<<endl <<">";
    cin>>selection;
    cin.ignore();
    switch(selection)
    {
        case 1:
            dragon.dismi();
            break;
        default:
            cout<<"Invalid input";
            break;  

    }

    cin.ignore();
    cin.get();
}

回答1:


You are reading from an istream without checking the result, which is an anti-pattern.

You should check the result if cin >> selection to see if an int could be read from the stream. If it couldn't then the cin stream will be in an error state, and further attempts to read from it will return immediately instead of blocking waiting for input.

if (cin>>selection)
{
     switch (selection)
     {
     // ...
     }
}
else
    throw std::runtime_error("Could not read selection");

If you add that check you can at least rule out stream errors, and can try to debug it further.




回答2:


Try adding a parameter to ignore(). Something like:

cin.ignore(100);

There might be more than one character backed-up in cin.

http://www.cplusplus.com/reference/istream/istream/ignore/



来源:https://stackoverflow.com/questions/35470658/cin-get-does-not-wait-for-a-keypress-even-with-cin-ignore-before-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!