好的,我现在已经学习了大约4天的C ++,这是我的第一个编程语言。所以这真正意味着我只有大约8个小时的编程经验,其中很多是阅读我的C ++书的介绍并弄清楚如何使用XCode。
无论如何,我的初学者C ++书要求我做以下事情:“写一个密码提示,只给用户一定数量的密码输入尝试,这样用户就不会轻易写出密码破解者。”
唯一的问题是我刚刚学会了循环,我认为这本书甚至没有涵盖如何限制尝试。有人可以帮忙吗?我见过this,但它对我来说太先进了,我不明白。这是代码:(真正基本的新代码...对不起,如果它侮辱你的情报)
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
string password;
while ( 1 )
{
cout << "Enter your username: " << endl;
cin >> username;
cout << "Enter your password: " << endl;
cin >> password;
if ( username != "billy" && password != "bob" )
{
cout << "Incorrect username/password combination. Please try again." << "\n" <<
endl;
}
else
{
break;
}
}
cout << "Access granted." << endl;
}
答案 0 :(得分:8)
while ( 1 ) { }
构造重复{}
内的任何内容到无穷大,除非你从循环中明确break
。这是一个循环,顺便说一句。
经过多次尝试后,你怎么能打破它?你可以有一个计数器,它会随着每次尝试而增加,并在极限处从循环中断:
if ( ++counter >= limit )
break;
或简单地在while内移动条件
while ( ++counter < limit )
或使用简单的for
循环或do {} while()
。
答案 1 :(得分:0)
拿一些变量,比如tryCount,它记录了所做的尝试次数。将其初始化为0并在每次尝试失败时将其递增1。将条件置于while循环中,检查attemptCount是否小于允许的尝试次数(在下面的代码中取3)。所以,代码将是:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
string password;
int attemptCount = 0;
while ( attemptCount < 3 )
{
cout << "Enter your username: " << endl;
cin >> username;
cout << "Enter your password: " << endl;
cin >> password;
if ( username != "billy" && password != "bob" )
{
cout << "Incorrect username/password combination. Please try again." << "\n" <<
endl;
attemptCount++;
}
else
{
break;
}
}
cout << "Access granted." << endl;
}
答案 2 :(得分:0)
你的while(1)
循环将永远继续,除非你还有一些计数器,你在每次尝试失败时都会增加。
但坦率地说......为什么有一个while
循环和一个单独的计数器?您有一个已知的最大迭代次数;这是for
循环的一种情况。
for (int attempts = 1; attempts <= 3; ++attempts) {
... get login info ...
if (...username and password are correct...) {
cout << "Access granted.\n";
return 0;
}
else {
cout << "Invalid login.\n";
}
}
// Note as well, the default case (what happens if they make it through the loop)
// should be no access. Otherwise, someone could just succeed by inputting three
// bad passwords. :P
cout << "Too many invalid login attempts.\nExiting.\n";
return -1;
答案 3 :(得分:0)
想想这是如何运作的:
#include <iostream>
using namespace std;
int main()
{
const int MAXTRYS = 4;
int numTrys = 0;
while(numTrys != MAXTRYS)
{
cout << "Attempting login" << endl;
++numTrys;
}
return 0;
}
答案 4 :(得分:0)
您应该使用for
循环,而不是while
循环。有点像:
bool bSuccess = false;
for (int i = 0 ; i < maxAttemps ; ++i)
{
// your stuff
// set bSuccess = true when relevant
}
if (bSuccess)
{
// ok, successfully logged in
}
无限循环通常仅限于无限循环(在退出之前永远等待网络消息等)。作为良好实践的经验法则,尽可能避免无限循环,break
也会构造,因为它通常是一种hacky。要练习,你应该尝试编写一个很好的代码,转换成一个简单的数据流。
我猜你怀疑它,但这根本不安全(你在可执行文件的代码中用纯文本存储用户名和密码)。
答案 5 :(得分:0)
我很难重新熟悉C ++,因为高中(@ 8年前)已经发生了很多变化,或者我的信息学老师很糟糕...... 我也发现“for”循环对于这种练习更好,但是“返回0”并不是真的。和“打破;”做同样的事? 这就是我在这里看到的以及我已经“知道”的东西:)。像魅力一样。
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int attempts = 0;
string password;
for (int attempts = 0; attempts < 5; ++attempts )
{
cout << "enter your password! \n";
cin >> password;
++attempts;
if ( password == "boo123" )
{
cout << "granted!";
return 0;
}
else
{
cout << "denied! \n";
}
}
}
还有一件事:所有循环都是无限的'直到你“破碎”;它或“返回0;”它...
答案 6 :(得分:0)
/*Write a password prompt that gives a user only a certain number of password entry attempts—
so that the user cannot easily write a password cracker*/
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string password;
int x = 1;
while (1) //This creates an overall top level infinite loop
{
cout << "Input password here: ";
cin >> password;
if ( password == "teddy") //This sets the condition for success
{
cout << "Access Granted!!!!";
break; //The break is applied here to stop the cycle after success is made
}
else if ( password != "teddy") //This sets the condition for failure
{
cout << "Wrong username/password" << "\n" << x << " " << "wrong attempts" << "\n";
++x;
if ( x > 5 ) // This is the counter limit portion. Limit set to 5 attempts
{
break;
}
}
}
}
答案 7 :(得分:0)
#include <iostream>
using namespace std;
int main()
{
string password;
int pCounter = 0;
cout << "Enter Password here: ";
getline(cin, password);
while(pCounter <= 4){
if(password != "winner"){
cout << "Count: " << pCounter << endl;
cout << "Try again..wrong entry.." << endl;
cout << "Enter Password here: ";
getline(cin, password);
++pCounter;
if((password != "winner") && (pCounter == 4)){
cout << "The End..No more tries!!" << endl;
break;
}
}
else{
cout << "Welcome In Bro" << endl;
break;
}
}
return 0;
}
答案 8 :(得分:0)
使用命名空间标准;
int main(){ 字符串密码=“设置”; //声明密码 字符串输入//声明一个字符串供以后输入
for (int attempt = 1; attempt <= 3; attempt++) { //if you fail the password 3 times you get kicked out
cout << "enter password " << flush;
cin >> input;
if (input == password) { //checks to make sure the user input matches set password
cout << "granted";
return 0; //once correct password is put in the program ends
}
else { //if password is wrong repeat till right or 3 trys
cout << "you fail" << endl;
}
}
}