为什么我的程序停止在我告诉它的地方?

时间:2015-09-21 21:14:00

标签: visual-c++

我试图编写程序来清除屏幕并显示一个简单的帮助'用户要求的部分。然后该程序应该等待用户在再次清除屏幕之前按Enter键并重定向回到屏幕'他们以前(在不同的功能)。但是,由于某些原因,程序在继续之前不等待用户输入,并且帮助屏幕根本不显示。也许一组不同的眼睛可以看到我失踪的东西?

据我所知,cin.get();函数应该可以暂停我的程序,但它并没有做它应该做的事情。我希望问题实际上在帮助部分,但我不知道它是如何搞砸的。任何帮助将不胜感激,谢谢。

#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <windows.h>

using namespace std;

string Input;

bool EndGame = false;

void Help();

//You can ignore this function, I just included it because it's called multiple times
void Clear(int Cursorx, int Cursory)
{
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { Cursorx, Cursory };

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if (hStdOut == INVALID_HANDLE_VALUE) {return;}

    /* Get the number of cells in the current buffer */
    if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) {return;}
    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    /* Fill the entire buffer with spaces */
    if (!FillConsoleOutputCharacter(hStdOut,(TCHAR) ' ',cellCount,homeCoords,&count)) {return;}

    /* Fill the entire buffer with the current colors and attributes */
    if (!FillConsoleOutputAttribute(hStdOut,csbi.wAttributes,cellCount,homeCoords,&count)) return;

    /* Move the cursor home */
    SetConsoleCursorPosition( hStdOut, homeCoords );
}

void Help()
{       
    Clear(0,0);
    bool cont = false;
    cout <<"To move, use the 2,4,6,8 keys on the numpad or WASD"<< endl;
    cout << endl;
    cout <<"K - Stats"<< endl;
    cout <<"M - Map (once aquired)"<< endl;
    cout <<"J - Save"<< endl;
    cout << endl;
    cout <<"Press Enter to Continue"<< endl;

    cin.get();

    Clear(0,0);
}

void main()
{
    string Input;

    Center("THE DUNGEON");
    Center("type 'go' to start");
    Center("type 'load' to load previous game");
    Center("type 'h' to see all key commands");
    cout << endl;
    cout << endl;
    cout << endl;
    cin >> Input;

    /*if (Input == "test")
    {
        Test();
    }
    else if (Input == "go")
    {
        start();
    }
    else if (Input == "load")
    {
        Load();
    }*/
    else if (Input == "h")
    {
        Help();
    }

    if (EndGame == true)
    {
        Clear(0,0);
        Center("Thank you for Playing!  Return soon!");
    }

    Sleep(5000);
}

1 个答案:

答案 0 :(得分:0)

cin.ignore()之前添加cin.get()以摆脱输入缓冲区中的先前值。