how to do a password without cin?

I would like to make a program that see if i type a keys sequence, but i don't want to use cin, because i hide the console window
HERE THE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>

#define _WIN32_WINNT 0x0500

#include <windows.h>

int main()
{
int process=0;
/*ShowWindow(GetConsoleWindow(),SW_HIDE);  when my program will works, i hide the console*/
    while(true)
    {
        if(process==0)
        {
            if(GetKeyState('B') & 0x8000)
            {
                std::cout<<"b has been press"<<std::endl;
                process=1;
            }
            else
            {
                process=0;
                continue;
            }
        }
        if(process==1)
        {
               if(GetKeyState('C') & 0x8000)
               {
                   std::cout<<"c has been press"<<std::endl;
                   process=2;
               }
               else
                {
                    process=0;
                    continue;
                }
        }
        if(process==2)
        {
                   if(GetKeyState('Q') & 0x8000)
                   {
                       std::cout<<"Q has been press"<<std::endl;
                       process=3;
                   }
                    else
                    {
                        process=0;
                        continue;
                    }
        }
        if(process==3)
        {
                       if(GetKeyState('W') & 0x8000)
                       {
                           std::cout<<"W has been press"<<std::endl;
                           process=4;
                       }
                       else
                        {
                            process=0;
                            continue;
                        }
        }
        if(process==4)
        {
                           if(GetKeyState('L') & 0x8000)
                           {
                               MessageBox(NULL,"WORK","YES",MB_OK);
                               break;
                           }
        }
    }
ShowWindow(GetConsoleWindow(),SW_SHOW);
return 0;
}

The problem is that: if i press b my program output "b has been press" more or less fifty-three times, and after if i press c,nothing happen, but if i press still b the program output other fifty-three times "b has been press".
Sorry for my bad English
windows specific, with getch():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# include <iostream>
# include <conio.h>
# include <string>

int main()
{
    std::string password{};
    char c{};
    std::cout << "Enter password: \n";
    while (c != '\r')
    //carriage return (add others as required like newline, size of password etc)
    {
        c = getch();
        password.push_back(c);
    }

    std::cout << password << "\n";
}
Last edited on
Is this for a toy/schoolwork or a serious application? Hiding it on the screen is one thing, protecting it from other attacks is more complex.

If not on a compiler with <cornholio>, you can probably delete and replace the character with * faster than it can be seen, but that would be tricky, console programs are usually slow at updating text. You might find an answer in the console graphics extensions as well, *curses* family?

Gunnerfunner, the problem is that i want to hide the console window, getch works only in console.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

#ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
#endif // _WIN32_WINNT

#include <windows.h>

int main()
{
    int process = 0;

    // ::ShowWindow( ::GetConsoleWindow(), SW_HIDE );

    const char keys[] = "BCQW" ;

    while( process < 4 ) // B => C => Q => W
    {
        if( ::GetKeyState( keys[process] ) & 0x8000 )
        {
            std::cout << keys[process] << " has been pressed" << std::endl;
            ++process ;
        }
    }

    while( process == 4 ) // L
    {
        if( GetKeyState('L') & 0x8000 )
        {
            ::MessageBox( nullptr, "WORK", "YES", MB_OK );
            ++process ;
        }
    }

    // ::ShowWindow( ::GetConsoleWindow(), SW_SHOW );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

#ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
#endif // _WIN32_WINNT

#include <windows.h>

struct echo_off // raii wrapper for turning stdin echo off
{
    echo_off()
    {
        ::GetConsoleMode( h_stdin, std::addressof(mode) );
        ::SetConsoleMode( h_stdin, mode & ~ENABLE_ECHO_INPUT ) ; // disable echo
    }

    ~echo_off() { ::SetConsoleMode( h_stdin, mode ) ; } // restore previous mode
    echo_off( const echo_off& ) = delete ;
    echo_off( echo_off&& ) = delete ;

    const HANDLE h_stdin = GetStdHandle( STD_INPUT_HANDLE );
    DWORD mode = 0 ;
};

std::string getpass( std::string prompt = "password: " )
{
    std::string pword ;

    std::cout << prompt << std::flush ;
    {
        echo_off noecho ; // turn echo off; its destructor will restore it
        std::getline( std::cin >> std::ws, pword ) ;
    }

    return pword ;
}

int main()
{
    std::cout << "\ngetpass() returned: " << getpass() << '\n' ;
}
JLBorges thank you so much, first code works .
The second would also work. (Lines 19, 20 and 22 require C++11)
Topic archived. No new replies allowed.