【发布时间】:2013-10-25 00:53:25
【问题描述】:
我刚刚开始使用 C++,并且对 C# 有一定的经验,所以我一般都有一些编程经验。然而,似乎我马上就被击落了。我已经尝试在 Google 上查找,以免浪费任何人的时间而无济于事。
int main(int argc, char *argv[])
{
HANDLE hConsole;
int k = 5;
string h;
string password = "pass";
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
SetConsoleTextAttribute( GetStdHandle( STD_INPUT_HANDLE ), 0x5B );
while (h != password)
{
printf("This PC is locked\nEnter the password to gain access\n");
scanf("%s", &h);
}
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
每当我运行它时,它都会让我输入密码,当我单击 Enter 时,它会确认然后崩溃,要求我调试或向 Microsoft 发送信息。当我添加 while 循环检查两个字符串时,这开始了。我是否正确执行了此操作或遗漏了什么?
以防万一不清楚。我希望程序将字符串与输入进行比较,如果它们相同,程序将结束。
感谢收看。
【问题讨论】:
-
不要将
scanf用于C 字符串,尤其是std::string。使用std::cin(如果需要,可以结合std::getline)。 -
感谢您的反馈。