Can any1 share that how to make segment program when user entering a password,it will print * instead of the real character that user is entering?
Can any1 share that how to make segment program when user entering a password,it will print * instead of the real character that user is entering?
Basically your choices are 1. disable echoing of the console using an OS-specific function (For a Windows solution see here - c - How to disable echo in windows console? - Stack Overflow)
Or 2. Use an abstraction library like libreadline or ncurses which provides control over what gets echoed to the terminal in an OS-independent way.
thanks for your reply,but i dont understand.
Normally when the user types on the terminal, the input is "echoed" back to the output and displayed. If you want to display only a "*" for each character, then you must do two things:
1. turn off echoing of the input.
2. each time you read a character (with getchar() for example), printout a "*"
I gave two possibilities for solving issue #1. Does it make sense now? If not, which part is not clear.
you can try this code...
there are some problems with backspace function in this code so i removed it.Code:#include<iostream> #include<conio.h> #include<string.h> using namespace std; void test() { char pass[10],ch; int i; cout<<"enter the password(10 characters max.): "; for(i=0;i<10;i++) { pass[i]=getch(); if(pass[i]==13)//ASCII value of enter button (tested) { pass[i]='\0'; break; } else { cout<<"*"; } } if(strcmp(pass,"password")==0) { cout<<"\n\nthe password is correct!"; } else { cout<<"\n\nthe password is incorrect!!!"; } } int main() { test(); getch(); return 0; }
There are more problems than just the backspace function:
- You posted C++ code, and this is the C forum.
- Your code could use better indentation/formatting. Readability is key, easy to read code means fewer mistakes, and they're easier to find and fix. Also, some spaces around your operators would be nice, to also improve readability.
- You have an arbitrary, small limit for a password (9 chars), but you don't #define a constant for that, which is just bad style.
- Your program doesn't correctly handle the password. It allows you to fill the entire pass array with password chars, leaving no room for the null character. The missing null character means strcmp could go off the end of the array, resulting in undefined behavior (which is bad).
- You use cout for output, but getch for input. Don't mix C and C++ unnecessarily.
- Don't use magic numbers like 13. 13 (carriage return) is actually wrong in many cases (clearly you didn't test bit of code widely), other (non-Windows) systems use a new line character instead. Use the character constant '\n' which will work correctly for all line-endings in any text-mode stream. Besides, it's clearer, platform-independent and not ASCII-specific (yes, there are non-ascii systems out there).
- Other Windows-specific stuff that should be avoided unless you know the user wants a Windows-only solution.
- conio.h is old and outdated. Consider newer, more portable options.
- getch() is a Windows-specific function. Prefer the standard getchar() function instead. Again, there are modern, portable libraries that properly handle unbuffered terminal input, disabling echo, etc.
- Even if you should be using getch, you're doing it wrong. Read the documentation, it returns an int, not a char. A char is too small to handle all possible characters and special return values like EOF. Your code will mis-interpret things like EOF as some other char and not work correctly.
Here is windows code to look at: Problem with inputting password?
Making your own getch() for *nix style os's isn't too hard either: while getch question
gg
Thanks for all the reply
appreciated
I read this far too often (I know - don't read so much -- hilarity ensues, I'm sure):
So is the wheel, and pants. Both designs have had a good run, so far. For everything that conio.h ever did, it still does, and does it just fine. Not taking anything away from PDCurses, but let's not take anything away from conio.h either. Fair is fair. A lot of students visiting the site still have to work with conio.h, not PDCurses, etc.conio.h is old and outdated. Consider newer, more portable options.
1. People actually still wear pants and use wheels. The analogy is a bit off.
2. If someone uses conio.h in the code here, there's a good chance that the users can't test it because conio.h is dos only.
Nothing personally wrong with conio.h, but users of these function calls may be better off seeking the help of a "DOS Programming" forum.
Incorrect.
Wrong again. It works great in Windows 98, Windows2000, WindowsME, WindowsXP, and Windows 7.
2. If someone uses conio.h in the code here, there's a good chance that the users can't test it because conio.h is dos only.
You're basing this on your feelings, not on facts:Nothing personally wrong with conio.h, but users of these function calls may be better off seeking the help of a "DOS Programming" forum.
1) conio.h works on all DOS AND on the above listed Windows OS versions
2) it's standard fare and required for many schools in India, and elsewhere.
3) we get a lot of visitors who use it. We shouldn't be telling them to go to a DOS programming forum. Really, a "DOS programming forum"?? ROFL!
Exactly my sentiment. It doesn't matter to me if a particular school uses it. It's still DOS even if a certain operating system emulates it. Personally I'd use DOSBox to run such a program, but actually, why bother. If the program uses a library I don't want to install, then it's basically untestable.
I checked and if you install the APR library then you can use the apr_password_get function. Not sure of the details because I've never used it:
Apache Portable Runtime: General Purpose Library Routines
Your choice of what libraries to install. Just say no to conio
>>Just say no to conio
But there is no reason to say no. I've yet to come across a windows compiler that doesn't provide it. So I see nothing wrong with using something that is as "windows-portable" as the WinAPI. We could implement _getch() ourselves using the WinAPI - but there is no need since conio.h exists and is supported by windows compilers.
gg