C ++密码尝试限制 [英] C++ Password attempt limit

查看:109
本文介绍了C ++密码尝试限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to write a program that limits the password to three incorrect attempts then exits after 3 incorrect attempts. Here is the code I have thus far. I feel like I am really close to figuring this out but i cannot figure out what to do.

#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; 
}

推荐答案

不是使用void函数获取密码,而是返回一个布尔值,如果正确输入了密码(在if语句中),则返回true;如果尝试3次,则返回false.失败(循环之后).然后在您的主函数中处理所需返回的getPassword.

rather than use a void function for get password perhaps return a boolean value, true if the password was entered correctly (in the if statement) or false if 3 attempts failed (after the loop). Then in your main function deal with what getPassword returned as you need to.

类似

#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; 
}

这篇关于C ++密码尝试限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆