Command Line (cin) Hiding & Counter Using C++

Started by
16 comments, last by kuphryn 22 years, 4 months ago
Hi. I was wondering how to: 1) hide what a user types, but still read the keys. I am working on a program that is involves password protection. I need a way to asks for the pass, but not display it openly just when logging onto any OS. A good example is logging onto linux. It asks for the password, but it does not display what the user enters. 2) code a counter that uses the clock as a seed. For example let say this is the code: cout << "testing"; cout << "1 "; ------------------3 second delay cout << "2 "; ------------------3 second delay cout << "3 "; In other word, is there a way to code some type of delay or counter? Thanks, Kuphryn
Advertisement
you are asking two different questions ... but as for the second one ... the only way to do this in a standard c / c++ single threaded app (as far as i know) ... is to spin in a loop ... peeking at the input buffers using a command that does not wait for input ... and getting the system time ...

here''s the psuedo code:

NORMAL (waiting version)

  cout << ....cout << ....cin >> ...cout << ...  


now inheirantly in a single threaded app each statement only gets executed after the lines above it have completed ... and since cin waits (blocks) until input is received it is impossible to wait for a time to expire in this system.

TIMED VERSION
  cout << ...cout << ...bool done = false;bool inputIsValid = false;while(!done)	{	current time = get system time	peek at input 		// I don''t know of any standard c commands for this but		// the dos and unix C libraries should include console functions		// that do not block	if(got input)		{		reset last time to current time		if(got ALL input)			{			inputIsValid = true;			done = true;			}		}	if(current time >= expiration time)		done = true;	}...now your code is here and you can test inputIsValid to see if the usertypes the input or if time expired ...  
1. cin.get();

2. High-performance timing is platform specific; under Windows look into timeGetTime, QueryPerformanceCounter/QueryPerformanceFrequency.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Thanks.

A few members posting other other boards about time_t and sleep(seconds) also.

Kuphryn
Thanks.

About function:

getch();

Is it one character per? If yes, it would need to be in a loop structure

Kuphryn
quote:Original post by Kuphryn
getch();

Is it one character per?

Yes.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Okay.
I tried implementing:

getch();

and

getche();

VC++ 6 responded with:

c:\x.cpp(179) : error C2039: ''getch'' : is not a member of ''basic_istream >''

Kuphryn
getch() is found in conio.h . It is not part of the iostream classes; it is an old C function.
Okay thank.

Does anyone know what std:: do I need to include in addition to:

#include
using std::???

I am still getting the error that getch() does not exist even when I include conio.h.

Kuphryn
getch is not a part of the Standard C++ Library, so it is not included in the standard namespace (ie you don''t have to do "using std::anything" to use it).
#include <conio.h>#include <iostream>#include <string>using namespace std;  // I know namespace pollution is evil, but I''m lazy                      //  and haven''t figure out where getline() is.int main(int argc, char *argv[]){  cout << "Enter a string of text and see it displayed. Enter \''Q\'' to quit." << endl;  char c;  int n = 0;  while(((c = getch()) != ''q'') && (c != ''Q''))  {    cout << "String " << ++n;    getline(cin, str);    if(str == "q" || str == "Q")      break;    cout << str;  }  return 0;} 

This works for me.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement