Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;密码尝试限制_C++_Passwords_Login Attempts - Fatal编程技术网

C++ C++;密码尝试限制

C++ C++;密码尝试限制,c++,passwords,login-attempts,C++,Passwords,Login Attempts,我正在尝试编写一个程序,将密码限制为三次错误尝试,然后在三次错误尝试后退出。这是我到目前为止的代码。我觉得我真的很快就能弄明白这一点,但我不知道该怎么办 #include <fstream> #include <iomanip> #include <iostream> #include <string> #include <cmath> using namespace std; void getPassword() { for

我正在尝试编写一个程序,将密码限制为三次错误尝试,然后在三次错误尝试后退出。这是我到目前为止的代码。我觉得我真的很快就能弄明白这一点,但我不知道该怎么办

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

void getPassword() 
{ 
  for(int i = 0; i < 3; i++)
  {
    string password; 
    cout << "Enter the password: "; 
    getline(cin, password); 

    if (password == "12345") break; 
      cout << "INVALID. "; 
  } //for
} // getPassword 
int main ()
{
  if (!getPassword()) break;
  else break;
  cout <<endl; 

  ofstream fout; 
  fout.open("mort.txt", ios::app);
  if (!fout.good()) throw "I/O error";

  double p; //Principal/Mortgage Amount
  cout << "What's the mortgage amount?";
  cin >> p; 
  cin.ignore (1000, 10);

  double r; 
  cout << "What's the annual interest rate?";
  cin >> r ;
  cin.ignore (1000, 10);
  double a = r / 100;
  double i = a / 12;

  double n = 30 * 12; //Number of payments per month
  double t = (p*(pow (1+i, n))*i) / ((pow(1+i, n)) -1);// monthly payment formula 

  cout<< fixed;
  cout<< setprecision(2);

  cout << "Mortgage Amount: "<<"$"<< p <<endl;
  cout << "Interest Rate: "<< r <<"%"<<endl;
  cout << "Term Years: "<< "30 " << "Years" <<endl;
  cout << "Monthly Payment: " <<"$"<< t <<endl; 

  fout << "Mortgage Amount: "<<"$"<< p <<endl;
  fout << "Interest Rate: "<< r <<"%"<<endl;
  fout << "Term Years: "<< "30 " << "Years" <<endl;
  fout << "Monthly Payment: " <<"$"<< t <<endl;
  fout <<endl; 

  fout.close();
  return 0; 
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void getPassword()
{ 
对于(int i=0;i<3;i++)
{
字符串密码;

cout与其使用void函数获取密码,不如返回一个布尔值,如果密码输入正确(在if语句中),则返回true;如果三次尝试失败(在循环之后),则返回false。然后在主函数中,根据需要处理getPassword返回的内容

大概是

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;
#include <cmath>
bool getPassword() 
{ 
    for(int i = 0; i < 3; i++)
    {
    string password; 
    cout << "Enter the password: "; 
    getline(cin, password); 

    if (password == "12345") return true; 
    cout << "INVALID. "; 
     } //for
return false;
} // getPassword 

int main ()
{
  if (!getPassword()) return 1;
  cout <<endl; 

  ofstream fout; 
  fout.open("mort.txt", ios::app);
  if (!fout.good()) throw "I/O error";

  double p; //Principal/Mortgage Amount
  cout << "What's the mortgage amount?";
  cin >> p; 
  cin.ignore (1000, 10);

  double r; 
  cout << "What's the annual interest rate?";
  cin >> r ;
  cin.ignore (1000, 10);
  double a = r / 100;
  double i = a / 12;

  double n = 30 * 12; //Number of payments per month
  double t = (p*(pow (1+i, n))*i) / ((pow(1+i, n)) -1);// monthly payment formula 

  cout<< fixed;
  cout<< setprecision(2);


  cout << "Mortgage Amount: "<<"$"<< p <<endl;
  cout << "Interest Rate: "<< r <<"%"<<endl;
  cout << "Term Years: "<< "30 " << "Years" <<endl;
  cout << "Monthly Payment: " <<"$"<< t <<endl; 


  fout << "Mortgage Amount: "<<"$"<< p <<endl;
  fout << "Interest Rate: "<< r <<"%"<<endl;
  fout << "Term Years: "<< "30 " << "Years" <<endl;
  fout << "Monthly Payment: " <<"$"<< t <<endl;
  fout <<endl; 

  fout.close();
  return 0; 
}
#包括
#包括
#包括
#包括
使用名称空间std;
#包括
bool getPassword()
{ 
对于(int i=0;i<3;i++)
{
字符串密码;

cout与其使用void函数获取密码,不如返回一个布尔值,如果密码输入正确(在if语句中),则返回true;如果三次尝试失败(在循环之后),则返回false。然后在主函数中,根据需要处理getPassword返回的内容

大概是

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;
#include <cmath>
bool getPassword() 
{ 
    for(int i = 0; i < 3; i++)
    {
    string password; 
    cout << "Enter the password: "; 
    getline(cin, password); 

    if (password == "12345") return true; 
    cout << "INVALID. "; 
     } //for
return false;
} // getPassword 

int main ()
{
  if (!getPassword()) return 1;
  cout <<endl; 

  ofstream fout; 
  fout.open("mort.txt", ios::app);
  if (!fout.good()) throw "I/O error";

  double p; //Principal/Mortgage Amount
  cout << "What's the mortgage amount?";
  cin >> p; 
  cin.ignore (1000, 10);

  double r; 
  cout << "What's the annual interest rate?";
  cin >> r ;
  cin.ignore (1000, 10);
  double a = r / 100;
  double i = a / 12;

  double n = 30 * 12; //Number of payments per month
  double t = (p*(pow (1+i, n))*i) / ((pow(1+i, n)) -1);// monthly payment formula 

  cout<< fixed;
  cout<< setprecision(2);


  cout << "Mortgage Amount: "<<"$"<< p <<endl;
  cout << "Interest Rate: "<< r <<"%"<<endl;
  cout << "Term Years: "<< "30 " << "Years" <<endl;
  cout << "Monthly Payment: " <<"$"<< t <<endl; 


  fout << "Mortgage Amount: "<<"$"<< p <<endl;
  fout << "Interest Rate: "<< r <<"%"<<endl;
  fout << "Term Years: "<< "30 " << "Years" <<endl;
  fout << "Monthly Payment: " <<"$"<< t <<endl;
  fout <<endl; 

  fout.close();
  return 0; 
}
#包括
#包括
#包括
#包括
使用名称空间std;
#包括
bool getPassword()
{ 
对于(int i=0;i<3;i++)
{
字符串密码;

如果(!getPassword())返回
void
,则不能执行
if(!getPassword())
甚至可以编译
即使编译时
getPassword
返回
void
?除了bool之外还有其他选项吗?如果这是您的意思,您也可以使用整数。或者您可以调用exit(1)在getPassword函数中,但在本例中有点不整洁。@David还有其他选项。但这就像有一张纸和剪刀并说“我想剪这张纸。我有一个专门为此设计的工具,但我想要其他选项。”:-)除非你有令人信服的理由不使用bool,否则我会使用bool。我们在我的编程书中还没有提到这一点。我不想因此而得到停靠点。@David:你只需使用
int
,返回1表示“密码已接受”,返回0表示“密码三次错误”。除了bool之外还有其他选项吗?如果你是这个意思,你也可以使用整数。或者你可以调用getPassword函数中的exit(1),但在这种情况下这有点不整洁。@David还有其他选项。但这就像拿着一张纸和剪刀说“我想剪这张纸。我有一个专门为此设计的工具,但我想要其他选项。”:-)除非你有令人信服的理由不这样做,否则我会使用bool。我们在我的编程书中还没有提到过这一点。我不想因此而得到停靠点。@David:你可以简单地使用
int
,返回1表示“密码已接受”,返回0表示“密码已接受”密码错误三次”。