C++ Password attempt limit

♀尐吖头ヾ 提交于 2020-01-07 07:08:29

问题


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

回答1:


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.

so something like

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


来源:https://stackoverflow.com/questions/8439004/c-password-attempt-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!