Terminates loop by pressing enter

A program that takes input from user(in form of hidden characters) and compares it with predefined array.. then tells whether the entered input valid or invalid.

Problem: i am unable to break for loop by hitting 'enter button'. Although characters are hidden and comparing but how to stop loop when user hits 'enter' after password.. user can enter 6 or more values that will be compiled as invalid soo.. help me plz

code
#include <iostream.h>
#include<conio.h>
void main()
{clrscr();
char arr[]="farazi";
char arr2[25];
char result;
cout<<"Enter Password ";
for(char i=0;i<=25;i++)
{
arr2[i]=getch();
cout<<"*";
}
result=memcmp(arr2,arr,6);
if(result==0)
{cout<<"\nPassword correct";}
else
{cout<<"\nInvalid Password";}
getch();
}
Can I start by having you change a few semi-unlrelated things first?

- First, please use the code tags, they are the "<>" symbol in under the format section to your right.

- Next main(...) should always return an integer never a void. This is part of the current C++ standard.

- You should have using namespace std; in the global scope of your code since you are writing as if it is there. Otherwise prefixing the io streams with "std::" would work as well.

- You should flush the input stream before using "getch()" to hold your program open at the end there. This insures that it will not grab a leftover character that was not extracted properly.
Last edited on
i m not getting u as i am new to c++ sorry!!
could u plz explain it by writing code?? that will be really helpful for me..
i am using Turbo C actually
Is there a reason you're using something that is so out of date?
Topic archived. No new replies allowed.