I've been trying to write the same thing for hours, every different way I could think of, but none work. I want to enable the user to type in a password, which on screen will be asterisked, and stop the loop when he hits enter. I tried this :
But this keeps going until I've entered 10 chars. Why doesn't the line "x = 11" break the loop next time around ?Code:char pass[30];
int main()
{
cout << "Password ?" << endl;
for(int x = 0; x < 10; x++)
{
pass[x] = getch();
cout << "*";
if(pass[x] == '\n')
{
pass[x] = '\0';
x = 11;
}
}
clrscr();
cout << endl << endl << pass;
cin.get();
return 0;
}
I also tried something like this :
[code]
char pass[30];
int x = 0;
void getpass()
{
pass[x] = getch();
switch(pass[x])
{
case '\n':
cout << "*";
pass[x] = '\0';
break;
default:
cout << "*";
x += 1;
getpass();
break;
}
This continues on for ever. Any ideas ?