0

i want the user to input a passwort. of course it's a secret passwort so nobody should see it. so i tried to replace the letters and numbers the user inputs, with ' * '. here is my try.

while ((pw=getch())!='x'){
cout << "*";
strcpy(pwstring,pw);

}

input_pw=atoi(pwstring.c_str());

later i want the 'x' to be a 'enter'. but at the moment it's not important. with this, i get some compiler errors under Visual Studio.

Fehler  3   
error C2664: 'strcpy': Konvertierung des Parameters 1 von 'char' in 'char *' nicht möglich  c:\users\tim\desktop\kalssnne\methoden.h    zeile: 70

i will try to translate this.

error 3
error C2664: 'strcpy': converting of parameter 1 from 'char' to 'char*' is not possible.



  official english error code
    "'function' : cannot convert parameter number from 'type1' to 'type2'" 

thank u:  R. Martinho Fernandes

but what does this mean, and how can i fix it?

hope u can help me

greetings.

Jonas
  • 121,568
  • 97
  • 310
  • 388
globus243
  • 710
  • 1
  • 15
  • 31

4 Answers4

4

Your question isn't as much about C++ as it is about how to interact with your terminal. The language is (deliberately) entirely agnostic of how input and output are handled, and everything that you're worried about is how the terminal behaves. As such, any answer will depend heavily on your platform and your terminal.

In Linux, you will probably want to look into termios.h or ncurses.h. There's an old Posix function getpass() which does something similar to what you want, but it's deprecated.

Unfortunately I have no idea how to approach terminal programming in Windows.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Oddly, I can find several source that claim `getpass` is obsolete, but I can't find an agreed upon replacement. – dmckee --- ex-moderator kitten Sep 13 '11 at 16:45
  • 1
    @dmckee: I think the problem is about concurrency and reentrancy. To read a password safely you have to set the terminal to raw mode, and perhaps that itself is tricky to get right in a concurrent setting. I think if you're single-threaded, `getpass()` should be OK. – Kerrek SB Sep 13 '11 at 16:49
  • That is a good call on the concurrency, the whole business is scary vulnerable if more than one thread of execution has access to the terminal. – dmckee --- ex-moderator kitten Sep 13 '11 at 16:53
3

On a posix system use getpass (3).

It won't give you asterix echos, instead it echos nothing, but it is the way to do it.

Or if you are on a BSD system you could use readpassphrase (3) which is more flexible than the older call.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
1

as R. Martinho Fernandes says: strcpy doesn't do what you think it does.

strcpy takes a char* buffer, and a char* source, and copies all of the data from the second (up to the first zero character) to the first. The easiest solution is to keep track of the length of pwstring and add characters one at a time:

char pwstring[100];
int length = 0;
while ((pw=getch())!='x' && length < 99){
    cout << "*";
    pwstring[length] = pw;
    length = length + 1;
}
pwstring[length] = '\0';
int pwint = atoi(pwstring);

[EDIT] If pwstring is a std::string, then this becomes REALLY easy, since it already keeps track of it's own length.

std::string pwstring;
while ((pw=getch())!='x'){
    cout << "*";
    pwstring += pw;
}
int pwint = atoi(pwstring.c_str());
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • maybe it works, but as shown above, i need this as an int-value. thats why i used atoi. and if u initiate pwstring with [100] my compiler says '.c_str()' must have class/struct/union. – globus243 Sep 13 '11 at 17:55
  • oh wow, I assumed from the use of strcpy() that pwstring was a char[?]. Re-reading your origional error, I see that it was actually a char, which isn't even remotely what you appear to have wanted. – Mooing Duck Sep 13 '11 at 17:58
-1
strcpy(pwstring,pw);

I'm guessing that pwstring is a std::string? strcpy is a c function, it acts on 'c' null terminated strings. You are providing it with a c++ string and an int.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263