c++密码尝试限制
C++ Password attempt limit
我试图写一个程序,将密码限制为三次错误的尝试,然后在3次错误的尝试后退出。这是我到目前为止编写的代码。我觉得我真的很接近解决这个问题,但我不知道该怎么做。
#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返回的内容。
所以像
#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++中的oop(密码生成)
- 密码登录程序将永远循环并显示不正确的结果
- OpenSSL没有共享密码
- Cryptopp:获取密码输入的填充字符串
- 密码长度验证 (c++)
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 仿射密码解密,输出大小写不同
- 替换密码:哪一个?
- 无法使用 openssl 服务器,因为密码已过时...什么的...ERR_SSL_VERSION_OR_CIPHER_MISMATCH
- 如何使用 c++ 在 sqlite3 中打开受密码保护的数据库?
- 根据用户名和密码 Qt C++重新访问数据库值
- C++密码问题
- 无法在 Cocos2dx 中使用受密码保护的 zip 文件
- 如何使用librdkafka设置明文协议(无SASL)的用户名和密码?
- 如何将凯撒密码的元素从字符串更改为字符串
- 如何在C++的凯撒密码程序中包含空格?
- 运行密钥密码解密知道密钥?
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- 当PSO细粒度策略对使用AdsGetObject MSDN API的Windows操作系统生效时,如何获取用户密码到期日
- 密码显示双字母 c++ 的问题