当用户输入错误的密码时,代码将在他们尝试两次代码停止时重新启动,但是在我的代码中,当用户输入错误的信息并且它通过了password_attempts时,它继续执行其他一切似乎是工作得很好
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myusername;
string mypassword;
bool Access_granted;
int password_attempts;
cout << "Enter your username: ";
cin >> myusername;
cout << "Enter your password: ";
cin >> mypassword;
if (myusername == "veasy62" && mypassword == "a65908") {
Access_granted = true;
cout << "Access granted veasy62\n";
}
else if (myusername == "tveasy62" || mypassword == "a1065908") {
Access_granted = true;
cout << "Access granted tveasy62\n";
}
else {
Access_granted = false;
cout << password_attempts;
cout << "Access Denied, Sorry try again\n";
if (Access_granted == false) {
if (password_attempts = 2) {
password_attempts = password_attempts + 1;
return main();
}
else {
cout << "Sorry you have ran out of attempts\n";
}
}
}
}
答案 0 :(得分:2)
你的代码得到了Litttle buggy。 而不是&amp;&amp;你用过了|| 在条件允许的情况下,= = 2的insteas = = password_attempts = 2 如果(错误检查条件)不必要 我删除了所有这些,这是您想要实现的任务的代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myusername;
string mypassword;
bool Access_granted;
int password_attempts=0;
HERE : cout << "Enter your username: ";
cin >> myusername;
cout << "Enter your password: ";
cin >> mypassword;
if (myusername == "veasy62" && mypassword == "a65908") {
Access_granted = true;
cout << "Access granted veasy62\n";
break;
}
else if (myusername == "tveasy62" && mypassword == "a1065908") {
Access_granted = true;
cout << "Access granted tveasy62\n";
break;
}
else {
password_attempts = password_attempts + 1;
Access_granted = false;
cout << password_attempts;
cout << "Access Denied, Sorry try again\n";
if (password_attempts == 2) {
cout << "Sorry you have ran out of attempts\n";
return 0;
//to exit main().can also use exit(0) using additional libraries
}
else{
goto HERE;
}
}
}
答案 1 :(得分:1)
如果你不想使用循环,你可以使用goto而不是返回。你可以使用“endl”而不是“\ n”。你可以试试这个 -
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myusername;
string mypassword;
bool Access_granted;
int password_attempts=0;
restart:
cout << "Enter your username: ";
cin >> myusername;
cout << "Enter your password: ";
cin >> mypassword;
if(myusername == "veasy62" && mypassword == "12")
{
Access_granted = true;
cout<<"Access granted veasy62" << endl;
}
else if (myusername == "tveasy62" || mypassword == "a1065908" )
{
Access_granted = true;
cout<<"Access granted tveasy62" << endl;
}
else
{
Access_granted = false;
if(Access_granted == false)
{
if(password_attempts < 2 )
{
cout << "Access Denied, Sorry try again" << endl;
password_attempts = password_attempts + 1;
cout << "you have remaining " << 2-password_attempts << " time chances after this attempt" << endl;
goto restart;
}
else
{
cout << "Sorry you have ran out of attempts\n";
}
}
}
return 0;
}
答案 2 :(得分:1)
所以你说你不能使用循环。那么这是一个使用递归模板的解决方案。没有&#34; gotos&#34;,可以通过将模板参数更改为更大的数字来轻松调整。
#include <string>
#include <iostream>
template <int attempts>
struct password_entry
{
static bool get_entry(std::string& myusername, std::string& mypassword)
{
std::cout << "Enter your username: ";
std::cin >> myusername;
std::cout << "Enter your password: ";
std::cin >> mypassword;
if (myusername == "veasy62" && mypassword == "12")
{
std::cout << "Access granted veasy62" << std::endl;
return true;
}
if ( attempts > 1 )
std::cout << "Access denied. You have " << attempts - 1 << " attempts remaining\n";
return password_entry<attempts - 1>::get_entry(myusername, mypassword);
}
};
template <>
struct password_entry<0>
{
static bool get_entry(const std::string&, const std::string&)
{
std::cout << "Sorry you have ran out of attempts\n";
return false;
}
};
int main()
{
std::string mypass, myuser;
// max 2 attempts
bool access_granted = password_entry<2>::get_entry(myuser, mypass);
if ( access_granted )
std::cout << "Welcome current user";
else
std::cout << "Please call 555-5555 to reset your password";
}
要调整尝试次数,只需更改password_entry模板参数调用。
编辑:调整后的代码不使用构造函数。
答案 3 :(得分:0)
所以我从你那里得知你想让用户输入他的密码两次,如果两者都错了,那么完成程序吧? 那么你可以这样写。
编辑:)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myusername;
string mypassword;
bool Access_granted;
int password_attempts=0;
cout << "Enter your username: ";
cin >> myusername;
cout << "Enter your password: ";
cin >> mypassword;
if (myusername == "veasy62" && mypassword == "a65908") {
Access_granted = true;
cout << "Access granted veasy62\n";
}
else if (myusername == "tveasy62" || mypassword == "a1065908") {
Access_granted = true;
cout << "Access granted tveasy62\n";
system("pause");
return 0;
}
else {
Access_granted = false;
cout << "Access Denied, Sorry try again\n";
cout << "Enter your username: ";
cin >> myusername;
cout << "Enter your password: ";
cin >> mypassword;
if (myusername == "veasy62" && mypassword == "a65908") {
Access_granted = true;
cout << "Access granted veasy62\n";
}
else if (myusername == "tveasy62" || mypassword == "a1065908") {
Access_granted = true;
cout << "Access granted tveasy62\n";
system("pause");
return 0;
}
else
{
cout << "Sorry you have ran out of attempts\n";
cout << "Access Denied";
system("pause");
return -1;
}
}
return 0;
}
答案 4 :(得分:0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myusername;
string mypassword;
bool Access_granted;
int password_attempts=0;
cout << "Enter your username: ";
cin >> myusername;
cout << "Enter your password: ";
cin >> mypassword;
if (myusername == "veasy62" && mypassword == "a65908") {
Access_granted = true;
cout << "Access granted veasy62\n";
}
else if (myusername == "tveasy62" || mypassword == "a1065908") {
Access_granted = true;
cout << "Access granted tveasy62\n";
}
else {
Access_granted = false;
cout << password_attempts;
cout << "Access Denied, Sorry try again\n";
if (Access_granted == false) {
if (password_attempts <=2) {
password_attempts = password_attempts + 1;
return main();
}
else {
cout << "Sorry you have ran out of attempts\n";
}
}
}
}
答案 5 :(得分:0)
使用转到 或者,如果您喜欢模块,请尝试使用递归函数。它将像您尝试的那样工作。
public int accessAllowed(int notrys=0)
{
if(notrys<2)
return 0;
string username,password;
//take username password
If(//condition 1)
return 1;
else if(//condition 2)
return 1;
else
return accessAllowed(notrys+1);
}
虽然goto会很有效率。
您的代码中还有很多错误