Hidden input (Clarify an answer on another question)

Can somebody please explain Guestgulkan's answer further in this question?

http://www.cplusplus.com/forum/beginner/43683/

I'm not understanding what each line of code is doing exactly. I need to be able to add a little to it. Basically it's turning echo off, but I need to take the input after that then turn echo back on, all in one function so I can call that function like you would cin.
Last edited on
Sth. like this?
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
#include <string>
#include <iostream>
#include <windows.h>

string ReadWithoutEcho()
{
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  DWORD mode = 0;
  GetConsoleMode(hStdin, &mode);
  SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

  string s;
  getline(cin, s);

  SetConsoleMode(hStdin, mode);
  return s;
}

int main() 
{
  cout << "Enter password ";
  string input = ReadWithoutEcho();
  cout << "The hidden password: " << input << "\n";
  int num;
  cout << "Enter num: " ;
  cin >> num;
  cout << "Num: " << num << "\n\n";
  system("pause");
  return 0;
}
to call it like cin you can wrap the function into a class, maybe even a functor if those are still in vogue, and overload the >> operator to do the silent read code. I haven't done a functor in so long I can't recall if you can overload an operator with them. A regular class will do for it, if not.

then you end up with something like

cins >> input; //cin secure, or cin silent? Name it what you will, of course.

Last edited on
@Thomas1965
yeah but both Windows and Linux versions of that. This is my biggest issue:

(psuedocode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

void/int/whatever hiddeninput()
{
   if windows
        { 
        "windows way"
        }
   else if linux
        { 
        "linux way"
        };
}

cout << "Password: "; hiddeninput();


I need to pretty much do that but I can't get linux to compile the windows stuff.

I'm thinking something like this might work with preprocessor directives:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#if defined(WINDOWS)
   //include windows stuff
   hiddeninput();
   {
      //"windows way" 
   }
#elif defined(LINUX)
   //include linux stuff
   hiddeninput();
   {
      //"linux way" 
   }
#endif

cout << "Password: "; hiddeninput();


Is it possible to define functions in a preprocessor directive?
I've tried with no luck but it may have been syntax or something.
@jonnin

I'm not good with classes yet but that may work for the preprocessor directive idea. maybe?

@everyone
I think that the preprocessor directives may be the only solution to this so basically at compile time the compiler will only look at the function for the OS doing the compiling. Either way it would end up with the same function but defined differently per OS, essentially creating a cross-platform source but a platform-dependent binary? What do you guys think?
Last edited on
O.K. I now have this:


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
#include <iostream>
#include <string>
#include <unistd.h>
#include <termios.h>

using namespace std;
string PASS;
   string getPASS()
   {
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    string PASS;
    cout << "Password: "; cin >> PASS; cout << endl;
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return PASS;
   }


int main()
{
   
   int attempts = 0;
   while (attempts == 3)
   {
      cout << "Too many attempts have been made! Exiting..." << endl; exit(0);
   };
   string USER;
   cout << "Username: "; cin >> USER;
   getPASS() = PASS;
   
   if (USER == "josh" and PASS == "hsoj")
   {
      cout << "\nAccess granted!\n" << endl;
      //do stuff
   } // End user (Starts at if)
   else
   {
      cout << "\nAccess denied!\n" << endl; 
      attempts = attempts + 1;
      main();
   };
   return 0;
}


The password input is not showing but it says access denied every time and the limit is no longer working, it just repeats the promt for username and password.

EDIT: I removed getPASS = PASS; and changed if (USER == "josh" and PASS == "hsoj") to if (USER == "josh" and getPASS() == "hsoj") and it works perfectly now!

EDIT 2: Also removed string PASS; from line 7 because it's no longer needed.
Last edited on
the preprocessor is aside.

The preprocessor will let you have 2 versions of the code, and swap between then when you compile on the various operating systems.

the class can take the above and abstract it into something that looks like cin so you can have the syntax consistency if you wanted that. This is just a code-pretty, calling a function to get the data is exactly the same thing. You might learn something from it, but its totally unnecessary fluff. These things can become necessary in huge projects where making the code easy to write and read is critical to making it work and getting it done.



Topic archived. No new replies allowed.