标题:如何去掉编译器自动输出的:“Press anykey to continue”这句话
只看楼主
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
可以的

授人以渔,不授人以鱼。
2013-08-10 00:20
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
程序代码:
#include <Windows.h>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <conio.h>

HANDLE hOutput;

const int K_BACKSPACE = 0x08;
const int K_ENTER     = 0x0D;

void Pause(void);
void ClearScreen(void);
void GotoXY(short x, short y);
void GotoXY(COORD pos);

void main(void)
{
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hOutput == INVALID_HANDLE_VALUE)
    {
        return;
    }

    ClearScreen();
    const size_t length = 8;
    char message[] = "请输入密码: ";
    COORD pos = { 5, 5 };
    GotoXY(pos);
    printf_s("%s________", message);
    GotoXY(pos.X + strlen(message), pos.Y);
    int ch;
    size_t counter = 0;
    char password[length];
    do
    {
        ch = _getch();
        switch (ch)
        {
            case 0xE0:
                ch = _getch();
                break;
            case K_BACKSPACE:
                --counter;
                CONSOLE_SCREEN_BUFFER_INFO info;
                GetConsoleScreenBufferInfo(hOutput, &info);
                GotoXY(info.dwCursorPosition.X - 1, info.dwCursorPosition.Y);
                _putch('_');
                GotoXY(info.dwCursorPosition.X - 1, info.dwCursorPosition.Y);
                break;
            default:
                if (isalnum(ch))
                {
                    password[counter++] = ch;
                    _putch('*');
                    if (counter >= length)
                    {
                        ch = K_ENTER;
                    }
                }
                break;
        }
    } while (ch != K_ENTER);
    password[counter] = '\0';
    GotoXY(pos.X, pos.Y + 1);
    printf_s("您输入的密码是: %s", password);

    Pause();
}

void Pause(void)
{
    GotoXY(0, 24);
    printf_s("Press any key to continue...");
    _getch();
}

void ClearScreen(void)
{
    CONSOLE_SCREEN_BUFFER_INFO info;
    DWORD number;
    COORD pos = { 0, 0 };
    GetConsoleScreenBufferInfo(hOutput, &info);
    FillConsoleOutputCharacterA(hOutput, ' ', info.dwSize.X * info.dwSize.Y, pos, &number);
    GotoXY(pos);
}

void GotoXY(COORD pos)
{
    SetConsoleCursorPosition(hOutput, pos);
}

void GotoXY(short x, short y)
{
    COORD pos = { x, y };
    SetConsoleCursorPosition(hOutput, pos);
}


[ 本帖最后由 TonyDeng 于 2013-8-10 01:21 编辑 ]

授人以渔,不授人以鱼。
2013-08-10 01:18
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
得分:0 
回复 12楼 TonyDeng
T版主好厉害,

我可得仔细看看这代码啦~~


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2013-08-10 14:38
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
--counter那里有个漏洞,要拦住光标退到最左边才行,你自己改。

授人以渔,不授人以鱼。
2013-08-10 20:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-418718-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.276094 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved