Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kuphryn

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

This topic is 6057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 user
types the input or if time expired ...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks.

About function:

getch();

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

Kuphryn

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!