-
Password Help
So, ive been trying to implement a password into my program. I have done a search through the forums on the weekend and found a great thread, but i cant seem to find it any more :(. Here is what i remember of it:
Code:
#include<iostream.h>
#include<string.h>
int main()
{
short x=0;
char password[20];
char ch;
cout<<"Enter password: ";
while(ch!=13)
{
ch=getch();
password[x]=ch;
cout<<"*";
x++;
}
password[x]='\0';
cout<<password;
return 0;
}
So when i ran it, it didnt show the *s. Then when it printed out the password, there was a whole bunch of crazy characters in front of the password i typed. Also, for a test, i printed out the length of the string and it was like 24 when i inputted like 3 or 4 characters. I also printed out the x value and it was one more then the number of characters i inputted. it probably got the enter key when i pressed it. How can i fix that?
My question is, how can i make the *s appear on the same line? And why was it printing out the password wrong and the length?
-
1) <iostream>, not <iostream.h>
2) <string> or <cstring> (depends on what you want), not <string.h>
3) You don't need either file from 2).
4) You need <conio.h> though.
5) You need to flush cout after each character.
6) The additional character is the '\r' that occurs in an "\r\n"-pair as represents a new line in Windows.