C++ Program. Could someone help me with this please. Objectives: Use basic C++ c
ID: 3689635 • Letter: C
Question
C++ Program. Could someone help me with this please.
Objectives:
Use basic C++ constructs: if, switch, and repetition (looping), function and array
Perform simple data type manipulations using array and string
Solve problem, design solution and implement using C++
Description:
Write a program named “ManageMyPasswords.cpp” that accepts and saves passwords from command line arguments. It will validate the password using the following password rules:
Be at least eight (8) characters
Contain at least one uppercase letter ('A'-'Z').
Contain at least one lowercase letter ('a'-'z').
Contain at least one number digit ('0'-'9').
Use alphanumeric characters only ('a'-'z', 'A'-'Z', '0'-'9')
Cannot start with a number
Each line in the password file should contain:
Timestamp (in milliseconds)
The password
Here is an example of the content of the password file (e.g. “data.txt”)
1460648198 HelloWorld123
1460648219 Password456
1460648236 Secret789
1460648269 Hell0Wor1d
1460654881 Hello123
1460655295 Passw0rd
Supported Usage Syntax:
ManageMyPasswords -file password-file-name -create
ManageMyPasswords -file password-file-name -show
ManageMyPasswords -file password-file-name -add new-password
ManageMyPasswords -file password-file-name -find search-string
“-create” will create a new empty data file. It will give an error message if the file already exists.
“-show” will display the content of the given data file. It will give an error message if the data file does not exist.
“-add” will add the given password to the end of the given data file. The new password must meet the given password rules.
“-find” will print out all the passwords that contain the given search string
Requirements:
1. The program must produce the same expected output as provided in the sample run.
2. When there is an error detected such as invalid syntax or invalid password format, meaningful and complete error messages should be given to the user.
3. Command syntax checking includes:
- “-add” and “-find” are given without the value followed
- Only supported actions (“-add” or “-find”, etc.) are performed
4. Should give an error message when the user tries to create a new file and the file exists
5. The program should have good documentation
6. The program should be modular and has well-defined tasks/functions
7. The program should not have lots of redundant or similar codes. Please try to factor them
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
bool valid_pass(string pass){
if (pass[0] - 48 >= 0 && pass[0] - 57 <= 0) return false;
if (pass.length() < 8) return false;
bool upper = false;
bool lower = false;
bool number = false;
for (int i = 0; i < pass.length(); i++){
if (isalnum(pass[i]) == false) return false;
if (pass[i] - 48 >= 0 && pass[i] - 57 <= 0)
number = true;
else if (pass[i] - 97 >= 0 && pass[i] - 122 <= 0)
lower = true;
else if (pass[i] - 65 >= 0 && pass[i] - 90 <= 0)
upper = true;
}
if (lower == true && upper == true && number == true)
return true;
return false;
}
int main(int argc, char **argv){
if (argc >= 4){
string command = argv[3];
if (command == "-create"){
if (ifstream(argv[2]))
cout << "File is already there" << endl;
else{
ofstream outfile;
outfile.open(argv[2]);
outfile.close();
}
}
else if (command == "-show"){
if (ifstream(argv[2])){
ifstream infile;
infile.open(argv[2]);
string stamp,password;
while (!infile.eof()){
infile >> stamp >> password;
cout << stamp << " " << password << endl;
}
}
else{
cout << "NO SUCH FILE EXIST" << endl;
}
}
else if (command == "-add"){
string temp = argv[4];
if (valid_pass(temp) == true){
ofstream outfile;
outfile.open("test.txt", std::ios_base::app);
long ans = 1400500400 + rand() % 1000000000;
outfile << ans << " " << temp << endl;
outfile.close();
}
}
else if (command == "-find"){
string temp = argv[4];
if (ifstream(argv[2])){
ifstream infile;
infile.open(argv[2]);
string stamp,password;
cout << "password that contains " << temp << " are : ";
while (!infile.eof()){
infile >> stamp >> password;
size_t found = password.find(temp);
if (found != std::string::npos)
cout << password << " ";
}
cout << endl;
}
else{
cout << "NO SUCH FILE EXIST" << endl;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.