Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Ask Passwords using getch()

 
Reply to this topicStart new topic

Ask Passwords using getch()

Ricendithas
post 12 Jul, 2008 - 03:59 AM
Post #1


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 34


My Contributions


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.

Here is my code:

CODE
#include <stdio.h>
#include <conio.h>

int main(void) {
    int x;
    int y;
    
    while(x != 13) {
        y = y + " " + x;
        x = getch();
    }
    
    printf("\n\n\n%d\n\n", y);
    
    printf("\n\n\n%d", x);
    
    getch();
    return 0;
}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 12 Jul, 2008 - 04:23 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Use a character array. Check my snippets for more details, I have one called secure password entry in the console which uses getch.

It's written in C++ but you should hopefully be able to figure it out from that.

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

Ricendithas
post 12 Jul, 2008 - 04:28 AM
Post #3


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 34


My Contributions


QUOTE(gabehabe @ 12 Jul, 2008 - 05:23 AM) *

Use a character array. Check my snippets for more details, I have one called secure password entry in the console which uses getch.

It's written in C++ but you should hopefully be able to figure it out from that.

Hope this helps smile.gif



Yeah, I saw your program. It's nice, unfortunately, I'm not good in C++. I only know C.

Anyways, Thanks smile.gif

This post has been edited by Ricendithas: 12 Jul, 2008 - 04:29 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 12 Jul, 2008 - 05:04 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


OK, I've altered it to work as a C program.

The only problem is dynamic char arrays, so I'll leave that to you.

cpp
/***************************/
/** SECURE PASSWORD ENTRY **/
/** BY DANNY BATTISON **/
/** gabehabe@hotmail.com **/
/***************************/

#include <conio.h>
#include <windows.h>
#include <stdio.h>

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 smile.gif
User is online!Profile CardPM

Go to the top of the page

Ricendithas
post 12 Jul, 2008 - 04:22 PM
Post #5


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 34


My Contributions


QUOTE(gabehabe @ 12 Jul, 2008 - 06:04 AM) *

OK, I've altered it to work as a C program.

The only problem is dynamic char arrays, so I'll leave that to you.

cpp
/***************************/
/** SECURE PASSWORD ENTRY **/
/** BY DANNY BATTISON **/
/** gabehabe@hotmail.com **/
/***************************/

#include <conio.h>
#include <windows.h>
#include <stdio.h>

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 smile.gif



The program works. smile.gif 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 **/
/***************************/

#include <conio.h>
#include <windows.h>
#include <stdio.h>

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 */
}


Thanks for helping me smile.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 13 Jul, 2008 - 06:54 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Why not use strcmp(); like so:
cpp
/***************************/
/** SECURE PASSWORD ENTRY **/
/** BY DANNY BATTISON **/
/** gabehabe@hotmail.com **/
/***************************/

#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>

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 */
}

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

Ricendithas
post 13 Jul, 2008 - 05:02 PM
Post #7


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 34


My Contributions


QUOTE(gabehabe @ 13 Jul, 2008 - 07:54 AM) *

Why not use strcmp(); like so:
cpp
/***************************/
/** SECURE PASSWORD ENTRY **/
/** BY DANNY BATTISON **/
/** gabehabe@hotmail.com **/
/***************************/

#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>

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 */
}

Hope this helps smile.gif



Thanks a lot, it is finally working. smile.gif
User is offlineProfile CardPM

Go to the top of the page

polymath
post 14 Jul, 2008 - 08:39 AM
Post #8


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


I'd just use something easy like this: (in C++)
cpp

#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
User is offlineProfile CardPM

Go to the top of the page

Ricendithas
post 17 Jul, 2008 - 01:37 AM
Post #9


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 34


My Contributions


QUOTE(polymath @ 14 Jul, 2008 - 09:39 AM) *

I'd just use something easy like this: (in C++)
cpp

#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.

c

#include <stdio.h>
#include <iostream>
#include <string>
#include <conio.h>

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;
}


The using namespace std; was an error in C.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2008 - 05:59 AM
Post #10


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


C does not use namespaces...also, C does not have a string object, nor can the equality operator be used to compare strings in C.

The code above is a mix of c and C++ - it will not work on a C compiler.
User is offlineProfile CardPM

Go to the top of the page

polymath
post 17 Jul, 2008 - 09:16 AM
Post #11


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


sorry 'bout that. I take so much of this stuff for granted now... isn't there a string lib in C though (I've never used it, however)?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2008 - 09:25 AM
Post #12


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month