即使在使用cin.ignore()之前,Cin.get()也不等待按键 [英] Cin.get() does not wait for a keypress even with cin.ignore() before it

查看:57
本文介绍了即使在使用cin.ignore()之前,Cin.get()也不等待按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是位新手,正在使用下面的程序进行编程。无论出于何种原因,在我的程序结束时,编译器都会完全跳过cin.ignore()并直接移至cin.get(),但之前已经有按键操作,因此编译器会完全跳过它。无需等待按键即可完成程序。我尝试将cin.get()和cin.ignore()放在switch case语句中,但是会发生相同的错误。我已经在网上搜索了此问题,但找不到与我的问题有关的任何内容。这是我的全部代码:

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


使用命名空间std;
类暴民
{
public:
mobs();
〜mobs();
void define();
void dismi();
void getinfo();
int stat [2];
字符串名称;
string bio;受保护的



int健康;
int级别;

};
小怪龙;
mobs :: mobs()
{

int stat [2];

}
mobs ::〜mobs()
{


}

int选择;

void mobs :: dismi()
{
getinfo();
cout<<等级:<<等级<健康:<<<健康<< endl<<< endl<<< endl<< :<<名称<"生物:<<生物<< endl<< endl;

}

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

}

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

}


int main()
{
dragon.stat [0] = 100;
dragon.stat [1] = 13;
dragon.name =空灵龙,飞机龙;
dragon.bio =只能在空灵飞机上发现的龙。这条龙具有传统的能力,如飞行和呼吸能力。空灵龙的其他已知能力是瞬移或魔力。 cout<"欢迎使用暴民手册。<<< endl<< endl<<"请进行选择<<" endl;
cout<< 1。虚灵龙》<< endl<< 2。 << endl< endl<<> ;;
cin>选择;
cin.ignore();
switch(selection)
{
case 1:
dragon.dismi();
休息时间;
默认值:
cout<<无效的输入;
休息时间;

}

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


解决方案

尝试在 ignore()

  cin.ignore(100); 

cin 中可能备份了多个字符。 / p>

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


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();
}

解决方案

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/

这篇关于即使在使用cin.ignore()之前,Cin.get()也不等待按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆