将输入替换为“ *” C++
我希望用户输入密码。当然,这是一个秘密密码,因此任何人都不应该看到它。 所以我尝试用“*”替换用户输入的字母和数字。这是我的尝试。
while ((pw=getch())!='x'){
cout << "*";
strcpy(pwstring,pw);
}
input_pw=atoi(pwstring.c_str());
后来我希望“x”成为“enter”。但目前这并不重要。这样,我在 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
我会尝试翻译这个。
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
但这是什么意思,我该如何解决它?
希望你能帮助我
问候。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题与其说是关于 C++,不如说是关于如何与终端交互。该语言(故意)完全不知道如何处理输入和输出,而您担心的一切都是终端的行为方式。因此,任何答案都将在很大程度上取决于您的平台和终端。
在 Linux 中,您可能需要查看
termios.h
或ncurses.h
。有一个旧的 Posix 函数getpass()
,它可以执行与您想要的类似的操作,但它已被弃用。不幸的是我不知道如何在 Windows 中进行终端编程。
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
orncurses.h
. There's an old Posix functiongetpass()
which does something similar to what you want, but it's deprecated.Unfortunately I have no idea how to approach terminal programming in Windows.
在 posix 系统上使用 getpass (3)。
它不会给你星号回声,而是什么也不回声,但这是做到这一点的方法。
或者,如果您使用的是 BSD 系统,您可以使用 readpassphrase (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.
正如 R. Martinho Fernandes 所说:
strcpy 不会做你认为它会做的事情。
strcpy
需要一个char*
缓冲区和一个char*
源,并将第二个(直到第一个零字符)的所有数据复制到第一个。最简单的解决方案是跟踪 pwstring 的长度并一次添加一个字符:[编辑] 如果 pwstring 是 std::string,那么这变得非常容易,因为它已经跟踪了它自己的长度。
as R. Martinho Fernandes says:
strcpy doesn't do what you think it does.
strcpy
takes achar*
buffer, and achar*
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:[EDIT] If pwstring is a std::string, then this becomes REALLY easy, since it already keeps track of it's own length.
我猜 pwstring 是 std::string 吗? strcpy 是一个 ac 函数,它作用于 'c' 以 null 结尾的字符串。您为其提供一个 C++ 字符串和一个 int。
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.