CrazyEngineers
  • simple password program problem in c++

    sumit_goel

    Member

    Updated: Oct 25, 2024
    Views: 1.5K
    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<iostream.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    char pwd[5];
    int i=0;
    while(i<5)
    {
    pwd[i]=getch();
    cout<<"*";
    ++i;
    }
    cout<<pwd;
    if (pwd=="sumit")
    cout<<"correct password";
    else
    cout<<"wrong password";
    
    getch();
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberJun 2, 2010

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

    #include<iostream.h>
    #include<conio.h>
    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<<endl;
    mycheck=check(pwd,def);
    if (mycheck==1)
    cout<<"correct password"<<endl;
    else
    cout<<"wrong password"<<endl;
    
    getch();
    }
    
    int check(char a[5],char b[5])
    {int i;
        for(i=0;i<5;i++)
        {
            if(a[i]!=b[i])
            {
            break;
            }
        }
        if(i==5)
        return 1;
        else
        return 0;
    }
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJun 2, 2010

    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<iostream.h>
    #include<conio.h>
    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<<pwd; [COLOR=Blue]//Why are you printing the password. Here you should print a new line.[/COLOR]
    if (pwd=="sumit") [COLOR=Blue]// This will not work in a C program, use strcmp ().[/COLOR]
    cout<<"correct password";
    else
    cout<<"wrong password";
    
    getch();
    }
    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<stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJun 2, 2010

    @goyal420, which compiler did you use? Your program isn't compiling in VC++.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 3, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberJun 3, 2010

    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).
    <iostream> is standard, <iostream.h> 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.
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJun 3, 2010

    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".
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJun 3, 2010

    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).
    <iostream> is standard, <iostream.h> 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++, <iostream.h> is not correct (but works), it should be <iostrean>.

    I've used C, so in C you have to write <stdio.h>. 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++.
    Are you sure? This action cannot be undone.
    Cancel
  • ankesh.cs2007

    MemberJun 7, 2010

    i agree with gaurav bhorkar
    Are you sure? This action cannot be undone.
    Cancel
  • vignesh kumar selvakumar

    MemberApr 9, 2015

    #-Link-Snipped-# the password program is not working , in pwd some junk value is getting stored , pls help.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 9, 2015

    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<iostream.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    char pwd[5];
    int i=0;
    while(i<5)
    {
    pwd[i]=getch();
    cout<<"*";
    ++i;
    }
    cout<<pwd;
    if (pwd=="sumit")
    cout<<"correct password";
    else
    cout<<"wrong password";
    
    getch();
    }
    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<stdio.h>
    #include<conio.h>
    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);
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 9, 2015

    oh, my bad! didn't notice how old this post is
    Are you sure? This action cannot be undone.
    Cancel
  • mohit gahlot

    MemberFeb 1, 2016

    #include<iostream>
    #include<conio.h>
    #include<string.h>
    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<k;i++)
    {
    password.push_back(ch);
    }
    }
    // Try this, it is simple and easy
    Are you sure? This action cannot be undone.
    Cancel
  • rambaburai911

    MemberJan 16, 2017

    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    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();
    }
    Are you sure? This action cannot be undone.
    Cancel
  • rambaburai911

    MemberJan 16, 2017

    Hii I am a student of class12 science
    rambaburai911
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    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]
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register