CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@sumit_goel • 03 Jun, 2010 • 1 like
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();
}
@Manish Goyal • 03 Jun, 2010 • 1 like 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;
}

@gaurav.bhorkar • 03 Jun, 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.
@gaurav.bhorkar • 03 Jun, 2010 @goyal420, which compiler did you use? Your program isn't compiling in VC++.
@Manish Goyal • 03 Jun, 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
@Morningdot Hablu • 03 Jun, 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.
@gaurav.bhorkar • 03 Jun, 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".
@gaurav.bhorkar • 03 Jun, 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++.
@ankesh.cs2007 • 08 Jun, 2010 i agree with gaurav bhorkar
@vignesh kumar selvakumar • 09 Apr, 2015 @sumit_goel the password program is not working , in pwd some junk value is getting stored , pls help.
@Vishal Sharma • 09 Apr, 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);
}
@Vishal Sharma • 10 Apr, 2015 oh, my bad! didn't notice how old this post is
@mohit gahlot • 01 Feb, 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
@rambaburai911 • 16 Jan, 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();
}
@rambaburai911 • 16 Jan, 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]
28k views

Related Posts

@Ankita Katdare · Jun 3, 2015

What is IES or Indian Engineering Services Exam? Union Public Services Commission (UPSC) conducts the Engineering Services Exam or ESE 2016 is an entrance exam for graduates who wish to...
366.1k views

@autocrats · Mar 6, 2013

hi..everyone...i m participating in sae baja for the first time...i m designing the transmission..ive selected mahindra alpha transmission....i want to know whether it is good or do i need to...
2.3k views

@jaseemkannur · Jul 6, 2012

Can anyone help me in suggesting and choosing a good SHORT TERM course for B.E ECE graduate?Like networking...or anyother...also i have a doubt whether its necessary to take CISCO ONLINE...
1.6k views

@Ankita Katdare · May 30, 2014

Intex Mobiles has launched a new android smartphone in India today called the "Intex Aqua i5 Mini", a low budget phone with above average features. Sporting a 4.5" IPS FWVGA...
1.4k views

@Kaustubh Katdare · Apr 19, 2015

A lot of people want to know about the WhatsApp business model. The app is so popular that it's already crossed about 800 million active users and yet, it's offered...
11.9k views