Join 132,602 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 957 people online right now. Registration is fast and FREE... Join Now!
Hello! I'm creating a simple program that will ask for the user password. But, instead of displaying the characters, I used getch(). However, I'm wondering if how I am going to get the characters entered by the user. Should I use and array? Thanks.
int main () { char input[255]; int i = 0; for(;;i++) /* Infinite loop, exited when RETURN is pressed */ { char temp; temp = getch (); /* Get the current character of the password */ if (GetAsyncKeyState (VK_RETURN)) /* If the user has pressed return */ { input[i] = '\0'; break; } input[i] = temp; printf("*"); /* Print a star */ } printf("%s",input); _getch(); return EXIT_SUCCESS; /* Program was executed successfully */ }
int main () { char input[255]; int i = 0; for(;;i++) /* Infinite loop, exited when RETURN is pressed */ { char temp; temp = getch (); /* Get the current character of the password */ if (GetAsyncKeyState (VK_RETURN)) /* If the user has pressed return */ { input[i] = '\0'; break; } input[i] = temp; printf("*"); /* Print a star */ } printf("%s",input); _getch(); return EXIT_SUCCESS; /* Program was executed successfully */ }
Hope this helps
The program works. But, I would like to know if how will I be able to verify if the user entered the correct password? I tried to use this code and added some conditions, but it turned out that even if I entered the correct password, the condition is still false.
CODE
/***************************/ /** SECURE PASSWORD ENTRY **/ /** BY DANNY BATTISON **/ /** gabehabe@hotmail.com **/ /***************************/
int main () { char input[255]; int i = 0; for(;;i++) /* Infinite loop, exited when RETURN is pressed */ { char temp; temp = getch (); /* Get the current character of the password */ if (GetAsyncKeyState (VK_RETURN)) /* If the user has pressed return */ { input[i] = '\0'; break; } input[i] = temp; printf("*"); /* Print a star */ } printf("%s", input);
if(input == "a") printf("\n\nCORRECT!"); else printf("\n\nWRONG!"); _getch(); return EXIT_SUCCESS; /* Program was executed successfully */ }
int main () { char input[255]; int i = 0; for(;;i++) /* Infinite loop, exited when RETURN is pressed */ { char temp; temp = getch (); /* Get the current character of the password */ if (GetAsyncKeyState (VK_RETURN)) /* If the user has pressed return */ { input[i] = '\0'; break; } input[i] = temp; printf("*"); /* Print a star */ } printf("%s", input);
if(!strcmp(input,"test")) printf("\n\nCORRECT!"); else printf("\n\nWRONG!"); _getch(); return EXIT_SUCCESS; /* Program was executed successfully */ }
int main () { char input[255]; int i = 0; for(;;i++) /* Infinite loop, exited when RETURN is pressed */ { char temp; temp = getch (); /* Get the current character of the password */ if (GetAsyncKeyState (VK_RETURN)) /* If the user has pressed return */ { input[i] = '\0'; break; } input[i] = temp; printf("*"); /* Print a star */ } printf("%s", input);
if(!strcmp(input,"test")) printf("\n\nCORRECT!"); else printf("\n\nWRONG!"); _getch(); return EXIT_SUCCESS; /* Program was executed successfully */ }
#include <iostream> #include <string> #include <conio.h> using namespace std; int main() { char a; string pass; cout << "Enter password, then press enter: "; do { a=getch(); pass+=a; cout << "*"; } while (a!='\n'); string validpass="helloworld\n"; if (pass==validpass); //rest of your code else cout << "\nInvalid Password. Press any key to exit. "; getch(); return 0; }
EDIT: I noticed you're using C. To make it a C program, just replace all the cout <<"" with printf("") and you'll be fine. Also, you should include stdio.h
This post has been edited by polymath: 14 Jul, 2008 - 08:40 AM
#include <iostream> #include <string> #include <conio.h> using namespace std; int main() { char a; string pass; cout << "Enter password, then press enter: "; do { a=getch(); pass+=a; cout << "*"; } while (a!='\n'); string validpass="helloworld\n"; if (pass==validpass); //rest of your code else cout << "\nInvalid Password. Press any key to exit. "; getch(); return 0; }
EDIT: I noticed you're using C. To make it a C program, just replace all the cout <<"" with printf("") and you'll be fine. Also, you should include stdio.h
There was an error when I tried to run the program.
using namespace std; int main() { char a; string pass; printf("Enter password, then press enter: "); do { a=getch(); pass+=a; printf("*"); } while (a!='\n'); string validpass="helloworld\n"; if (pass==validpass); //rest of your code else printf("\nInvalid Password. Press any key to exit. "); getch(); return 0; }
There is indeed sir - string.h. that library however does not contain a definition for a string object, it is a set of functions used for string manipulation and comparison. With no string object, there is also no corresponding overload of the == operator for comparison purposes. In C, strcmp() is used to compare two strings - both of which are char pointers.