simple password program problem in c++

hello friends
i m beginners of c++ and i want to make simple password program that will accept password from user and simultaneously display the stars("*") with the simple while loop.
i have written the code of this program but this is showing wrong result.Pls help me in solving this problem.
The Code :
#include
#include
void main()
{
clrscr();
char pwd[5];
int i=0;
while(i<5)
{
pwd[i]=getch();
cout<<"*";
++i;
}
cout<
                

Replies

  • Manish Goyal
    Manish Goyal
    try this code if you find difficulty in understanding this code
    then post it here

    #include
    #include
    int check(char a[],char b[]);
    void main()
    {
    clrscr();
    char pwd[5],def[]="sumit";
    int i=0,mycheck;
    cout<<"Please enter password:-";
    while(i<5)
    {
    pwd[i]=getch();
    cout<<"*";
    ++i;
    }
    cout<
                                        
  • gaurav.bhorkar
    gaurav.bhorkar
    sumit_goel
    hello friends
    i m beginners of c++ and i want to make simple password program that will accept password from user and simultaneously display the stars("*") with the simple while loop.
    i have written the code of this program but this is showing wrong result.Pls help me in solving this problem.
    The Code :
    Your code:
    #include
    #include
    void main()
    {
    clrscr();
    char pwd[5]; [COLOR=Blue]// This should be of length 6, "sumit" measures 5 but one extra for '\0'[/COLOR]
    int i=0;
    while(i<5)
    {
    pwd[i]=getch(); [COLOR=Blue]/* There is no equivalent for getch() in std C++. you have to compile this as a C program */[/COLOR]
    cout<<"*";
    ++i;
    }
    cout<Here is my C equivalent. I used C because I wanted to use getch () which we cannot use in C++. Moreover, there is no equivalent function to getch () in C++.
    What getch () does is, taking a single keystroke as input. I tried using cin.get () in C++ which is equivalent to getchar() in C which takes a keystroke but waits for the Enter key.

    So, here is the C code,
    #include
    #include 
    #include 
    #include 
    int main()
    {
    system ("cls"); /[COLOR=Blue]/ clrscr () equivalent[/COLOR]
    char pwd[6]; [COLOR=Blue]// as i said, length should be 6 to accommodate the null character.[/COLOR]
    int i=0;
    while(i<5) [COLOR=Blue]//because the string is of length 5[/COLOR]
    {
    pwd[i]= getch ();
    printf ("*");
    ++i;
    }
    pwd[5] = '\0';[COLOR=Blue] // Null character which signifies the end of string.[/COLOR]
    printf ("\n");
    if (!strcmp (pwd, "sumit")) [COLOR=Blue]/* strcmp returns 0 if the strings are identical, hence the ! 
    operator, ie. the NOT logical operator. (so 0 becomes 1 i.e. True) */[/COLOR]
    printf ("correct password\n");
    else
    printf ("wrong password\n");
    
    system ("pause");[COLOR=Blue] // getch ()[/COLOR]
    }
    I hope you get it.
  • gaurav.bhorkar
    gaurav.bhorkar
    @goyal420, which compiler did you use? Your program isn't compiling in VC++.
  • Manish Goyal
    Manish Goyal
    I used C because I wanted to use getch () which we cannot use in C++.
    I don't agree with this statement .YES we can use this function

    It is available in conio.h file

    @goyal420, which compiler did you use? Your program isn't compiling in VC++.
    Borland C++ Compiler 5.5

    Is it really necessary to take extra memory for null character also ????
    I mean can't you take char pwd[5]; only
  • Morningdot Hablu
    Morningdot Hablu
    hello guy's
    up to what i know getch() is not an c or c++ function.
    it is a compiler extension. you can use it in either c or c++ if your compiler support it.
    @ goyal
    @gaurav
    here the header taken by you guy's are not standard(ANSI standered).
    is standard, is not

    >getch() is still available int the conio.h header file (at least in MinGW).
    I'm sure it works just fine on your compiler, but because it's non-standard, that means it's not guaranteed to work on my compiler, or someone else's compiler.
  • gaurav.bhorkar
    gaurav.bhorkar
    goyal420
    Is it really necessary to take extra memory for null character also ????
    I mean can't you take char pwd[5]; only
    Going by your program, taking pwd[5] works perfectly, because you have checked each and every character in check() function.
    But according to the definition of a string, the last character is the '\0' null character. It is required because the compiler (or std lib functions) should know where the string ends.

    Regarding the conio.h file, it is a non standard file written by Borland for use in Turbo C compilers. The modern compilers like GCC and VC++ doesn't support it. Try it, VC++ says, "cannot find conio.h".
  • gaurav.bhorkar
    gaurav.bhorkar
    mohit007kumar00
    hello guy's
    up to what i know getch() is not an c or c++ function.
    it is a compiler extension. you can use it in either c or c++ if your compiler support it.
    @ goyal
    @gaurav
    here the header taken by you guy's are not standard(ANSI standered).
    is standard, is not

    >getch() is still available int the conio.h header file (at least in MinGW).
    I'm sure it works just fine on your compiler, but because it's non-standard, that means it's not guaranteed to work on my compiler, or someone else's compiler.
    According to standard C++, is not correct (but works), it should be .

    I've used C, so in C you have to write . The extension .h is necessary.

    @goyal420: You program will definitely work in Turbo C/C++ compilers. No offense meant, your program is written in C++, but with a C accent. According to modern standards, main () in C++ should always return an int. You haven't used the namespace std. Thats why it is not working in VC++.
  • ankesh.cs2007
    ankesh.cs2007
    i agree with gaurav bhorkar
  • vignesh kumar selvakumar
    vignesh kumar selvakumar
    #-Link-Snipped-# the password program is not working , in pwd some junk value is getting stored , pls help.
  • Vishal Sharma
    Vishal Sharma
    sumit_goel
    hello friends
    i m beginners of c++ and i want to make simple password program that will accept password from user and simultaneously display the stars("*") with the simple while loop.
    i have written the code of this program but this is showing wrong result.Pls help me in solving this problem.
    The Code :
    #include
    #include
    void main()
    {
    clrscr();
    char pwd[5];
    int i=0;
    while(i<5)
    {
    pwd[i]=getch();
    cout<<"*";
    ++i;
    }
    cout<
    I wrote this code, kinda long back when I was new at CE. It's simple and written in C, shouldn't be much problem for you to convert that in C++. After all, there's some work even you should do instead of a simple copy paste 😀

    #include
    #include
    int main() {
        char pwd[50],c;
        int i = 0, done=0;
        while(!done) {
            c = getch();
            switch(c) {
                case '\r':
                    pwd[i] = '\0';
                    done = 1;
                    break;
                case '\b':
                    i--;
                    fputs("\b \b",stdout);
                    break;
                default:
                    pwd[i++] = c;
                    putchar('*');
            }
        }
        putchar('\n');
        fputs(pwd,stdout);
    }
    
  • Vishal Sharma
    Vishal Sharma
    oh, my bad! didn't notice how old this post is
  • mohit gahlot
    mohit gahlot
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    string password;
    char ch[10];
    int k=0;
    cout<<"Enter password\n";
    ch[k]=_getch();
    while(ch[k]!=13)
    {
    if(ch[k]!=8)
    {
    cout<<"*";
    k++;
    }
    else
    {
    k--;
    cout<<"\b \b";
    }

    ch[k]=_getch();
    }
    for(int i=0;i {
    password.push_back(ch);
    }
    }
    // Try this, it is simple and easy
  • rambaburai911
    rambaburai911
    #include
    #include
    #include
    void main()
    {
    clrscr();
    char *a,*b="ramramram";
    int I,r=0;
    ram :
    cout<<"enter your password:";
    for(I=0;l<9;l++)
    {
    a=getch();
    cout<<"*";
    }
    r=strcmp(a,b);
    if(r==0)
    cout<<"correct password";
    else
    {cout<<"incorrect password:";
    goto ram;}
    getch();
    }
  • rambaburai911
    rambaburai911
    Hii I am a student of class12 science
    rambaburai911
    #include
    #include
    #include
    void main()
    {
    clrscr();
    char *a,*b="ramramram";
    int I,r=0;
    ram :
    cout<<"enter your password:";
    for(I=0;l<9;l++)
    {
    a=getch();
    cout<<"*";
    }
    r=strcmp(a,b);
    if(r==0)
    cout<<"correct password";
    else
    {cout<<"incorrect password:";
    goto ram;}
    getch();
    }
    [/QUOTEI]

You are reading an archived discussion.

Related Posts

Hi all , Im new to this forum so a little bit of introduction : I am Gaurav a electronics and telecomm engineer , currently doing my internship from IIEC...
Hi all , I have hit a roadblock in my internship project. We are distributing 1 lakh CFLs to residential areas and are expected to measure the acutal usage of...
How do we make one-to-one links between 2 breeds in Netlogo 4.1 . Say i have a breed A and another breed B. I need one link running from every...
Hello friends... I have one doubt my deploying section... Anybody knew about to create setup file(.exe) for a java project.... Please...
Received following mail from ThoughWorks - We are delighted to invite you to an interactive webinar on 'Open Business Intelligence'. Business Intelligence (BI) is one of the most abused terms...