Incorporating a Password Attempt Limit
Oct 24, 2010 at 11:13am UTC
Hi folks, i'm new to C++ (as my code will prove obvious)
Let me start out by saying i've searched both google and these forums to no avail...
I'm fresh out of ideas how to incorporate a password attempt limit to my program... my goal is to have my "getPassword" subprogram tell "main" weather or not 3 tries have occurred, and if so...weather to proceed executing the program or not. Here's what i have so far, clearly something is wrong (getting compiling errors) -- anyone have a hint for me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#include <cmath>
int getPassword()
{
int result; // attempts
string password;
for (result = 0; result < 3; result++)
{
cout << "Enter the TOP SECRET passcode: " ;
getline(cin, password);
if (password == "1273296" ) break ;
cout << "INVALID. " ;
} // for
return result;
} // getPassword
int main()
{
int getPassword();
if (getPassword() > 1) break ;
cout << "This program was built to..." << endl << endl;
double p;
cout << "What's the Mortgage Amount? " ;
cin >> p;
cin.ignore(1000, 10);
double interest;
cout << "What's the Annual Interest Rate? " ;
cin >> interest;
cin.ignore(1000, 10);
double i = interest / 100;
double r = i / 12;
double n = 360;
double M = ((p * pow(1 + r, n) * r) / (pow(1 + r, n) - 1));
cout << endl;
cout << fixed;
cout << setprecision (0);
cout << "Mortgage Amount = $" << p << endl;
cout << setprecision (3);
cout << "Annual Interest Rate = " << interest << "%" << endl;
cout << setprecision (0);
cout << "Amortization Period = " << n / 12 << " Years" << endl;
cout << setprecision (2);
cout << "The monthly payment will be $" << M << endl;
ofstream fout;
fout.open("mortgages.txt" , ios::app);
if (!fout.good()) throw "I/O error" ;
fout << "For A loan Amount of $" << p << ", at an interest rate of " << interest << "%, the monthly payment is $" << M << endl;
fout.close();
return 0;
}
Oct 24, 2010 at 4:01pm UTC
The code looks good except the
break
statement in line 27. Write
return 0;
instead of break so the program will quit
You can also rewrite getPassword function that it will return false if user inputed password incorrectly for 3 times:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool getPassword()
{
int result; // attempts
string password;
for (result = 0; result < 3; result++)
{
cout << "Enter the TOP SECRET passcode: " ;
getline(cin, password);
if (password == "1273296" ) break ;
cout << "INVALID. " ;
} // for
if (result>2)
return false ; // return false if password was invalid for 3 times
else return true ;
} // getPassword
and in main you can write
if (!getPassword()) return 0;
Oct 24, 2010 at 11:37pm UTC
thanks for the quick reply -- this is exactly what i was looking for!
Topic archived. No new replies allowed.