为什么我的 cmd 消失了?我使用 cin.ignore(),但它没有效果!C++

Why my cmd disapears? I use cin.ignore() but it has no effect! C++

本文关键字:C++ 有效果 ignore 消失了 cmd 我的 cin 为什么      更新时间:2023-10-16

我正在测试我的轮盘类,但视觉工作室不让我看到输出,因为 cin.ignore() 不起作用,我不知道这里发生了什么,

你能解释一下为什么我的cmd行窗口消失以及为什么这段代码中的cin.ignore()永远不会被调用吗?

非常感谢!

#include "Bet.h"
#include "Table.h"
#include <iostream>
using namespace std;
using namespace Roulette;

int main()
{
    cout << "Enter numbers for Split bet:" << endl;
    short answer;
    vector<short>* Selection = new vector<short>;
    for (short i = 0; i < 2; ++i)
    {
        cin >> answer;
        Selection->push_back(answer);
    }
    cout << "Enter how many chips and chip worth:" << endl;
    short chips, worth;
    cin >> chips >> worth;
    Bet* MyBet = new Bet(TableLayout::European, BetName::Split, chips, worth, Selection);
    Bet* Complex = new Bet(TableLayout::European, BetName::VoisinsDeZero, 1, 1);
    Complex->PrintProperties();
    cin.ignore(); // THIS IS IGNORED!!! WHY??? please...
    delete Selection;
    Selection = nullptr;
    delete Complex;
    delete MyBet;
    return 0;
}

编辑:

我按 F5 运行程序,然后键入数字,如下所示:

1 输入2 输入1 输入1 输入

在这里cmd消失了,程序跳过cin.get()(或cin.ignore())任何它不起作用的东西。

CIN>>筹码>>值得;

例如,在此行中,如果键入最后一个1并按 Enter 键,则流中仍会保留 A n未读状态。

当你执行cin.ignore();时,它会提取剩余的字符,程序完成。将cin.ignore();更改为cin.ignore(2);,它应该可以工作。