【发布时间】:2016-01-16 02:23:24
【问题描述】:
这是我得到的错误。我刚刚从 python 转到 c++,这是一个巨大的调整。这是程序的描述:编写程序,使其创建 3 个或更多有效的用户名/密码组合,然后提示用户输入用户名和密码。如果组合有效,则打印确认消息。如果组合在第一次尝试时无效,则打印一条警告消息并让用户再试一次(再次提示他们输入用户名和密码)。如果第二次尝试正确,则打印确认消息。如果第二次尝试不正确,请打印一条指责消息。在任何一种情况下,在第二次尝试后停止程序。显示三种情况的输出:第一次尝试正确的用户名/密码;第二次尝试正确;两次尝试都不正确。
$ g++ username_password.cpp -o username_password
username_password.cpp: In function ‘int main()’:
username_password.cpp:34:2: error: ‘else’ without a previous ‘if’
else if (username != "Veasy62" && username != "tveasy62" && username != "terriyon62" && password != "a65908" && password != "a1065908" && password != "Aa1065908");
^
#include <iostream>
#include <string>
using namespace std;
int main()
{
int pswrd_attempts = 0;
string username;
string password;
cout << "Enter your username: " << "\n";
getline( cin, username, '\n' );
cout << "Enter your password: " << "\n";
getline( cin, password, '\n' );
while (username != "Veasy62" && username != "tveasy62" && username != "terriyon62" && password != "a65908" && password != "a1065908" && password != "Aa1065908");
{
pswrd_attempts++;
cout << "Enter your username: " << "\n";
getline( cin, username, '\n' );
cout << "Enter your password: " << "\n";
getline( cin, password, '\n' );
if (pswrd_attempts > 2)
{
cout << "You've ran out of attempts" << "\n";
}
}
else if (username == "Veasy62" && username == "tveasy62" && username == "terriyon62" && password == "a65908" && password == "a1065908" && password == "Aa1065908");
{
cout << "Correct password!" << "\n";
}
}
【问题讨论】:
-
我希望这些不是你的真实密码...
标签: c++ while-loop passwords