I need help with c++ please and please include Validate.h for the code part 2 Th
ID: 3836477 • Letter: I
Question
I need help with c++ please and please include Validate.h for the code part 2
This program will be creating a vector with logins made up of user names and passwords. I suggest writing this project in two parts to eliminate the frustration of trying to code/test too much code at one time.
Part I – Creating a vector composed of user names.
Write a C++ program that allows the user to input these string values:
1- User’s first name into firstName.
2- User’s last name into lastName.
The program places the first letter of the first name plus the first 5 characters of the last name into the string login. You can assume the last name is at least 5 characters.
The combined strings (login) will then be stored in a string vector.
See Display 8.4 for sample code concatenating strings, and Display 8.7 for a list of string functions.
Repeat the process of entering user names until the user enters 0 for firstName.
Output the list to the screen by displaying the vector items (the logins) one per line after building the vector. See lines 18-20 in 08.09.cpp.
//DISPLAY 8.9 Using a Vector
#include
#include
using namespace std;
int main( )
{
vector v;
cout << "Enter a list of positive numbers. "
<< "Place a negative number at the end. ";
int next;
cin >> next;
while (next > 0)
{
v.push_back(next);
cout << next << " added. ";
cout << "v.size( ) = " << v.size( ) << endl;
cin >> next;
}
cout << "You entered: ";
for (unsigned int i = 0; i < v.size( ); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
Part II – now add a password to the login after checking the password for errors.
Before adding the password to the login string, write a class named Validate (Validate.h) to validate the entry. The class should have the following functions:
o displayMsg Displays the messages in the message array so the user knows the rules for creating the password.
o checkLength Length of the string is at least 6 characters. Error message =: Password must be 6 characters. Returns true if the length is incorrect and false if the length is correct.
o checkSpaces Cannot contain a space. Error message =: Password cannot contain a space. Returns true if there are spaces and false if there are no spaces.
o checkUpper One character must be an upper case letter. Error message =: One letter must be upper case. Returns true if there are no upper case letters and false if there is an upper case letter.
The Validate class should contain:
Constructor that receives a string
Default constructor
These functions:
displayMsg
checkLength
checkSpaces
checkUpper
These variables/constants:
String entry;
const int LEN = 6; //use this constant in checkLength.
String array containing error messages. Use this array to display any messages required in the functions.
Here is the pseudocode for using the Validate class:
In the while loop in main, add a do loop that:
Displays the error messages.
requests a password.
instantiates an object using the constructor in Validate.
call the functions to validate the password.
repeat the loop if true is returned from any of the 3 called functions
Display any and all error messages.
After the password is validated, add a do loop to request the password a second time. This loop should verify that the first password entered is the same as the second entry. If not, display an error message and request the password again.
After the two passwords match, add the password string to the login before storing it in the vector.
login takes the form username, password
For example: pmadis, Abc123
Note the addition of the comma and space between the username and password.
//DISPLAY 8.9 Using a Vector
#include
#include
using namespace std;
int main( )
{
vector v;
cout << "Enter a list of positive numbers. "
<< "Place a negative number at the end. ";
int next;
cin >> next;
while (next > 0)
{
v.push_back(next);
cout << next << " added. ";
cout << "v.size( ) = " << v.size( ) << endl;
cin >> next;
}
cout << "You entered: ";
for (unsigned int i = 0; i < v.size( ); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
Explanation / Answer
1. Main
#include<iostream>
#include <string>
#include <vector>
#include "Validate.h"
using namespace std;
int main(){
string firstName, lastName,login;
vector<string> v;
int i;
while(1){
cin>>firstName;
if(firstName[0]=='0')
break;
cin>>lastName;
bool flag;
string pass;
Validate val=Validate("");
do{
val.displayMsg();
cout<<"Enter password"<<endl;
cin>>pass;
val=Validate(pass);
flag=val.checkLength();
if(flag)
continue;
flag=val.checkSpaces();
if(flag)
continue;
flag=val.checkUpper();
}
while(flag);
string pass2;
do{
cout<<"Enter the password again"<<endl;
cin>>pass2;
if(pass==pass2)
break;
cout<<"Passwords do not match"<<endl;
}
while(!(pass==pass2));
login=firstName+lastName[0]+lastName[1]+lastName[2]+lastName[3]+lastName[4]+", "+pass;
v.push_back(login);
}
for(i=0;i<v.size();i++)
cout<<v[i]<<endl;
cout<<endl;
}
2. Validate.h
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Validate{
private:
string entry;
int LEN;
string error_msg[10];
public:
// Validate(){
//}
Validate(string input){
entry=input;
LEN=6;
error_msg[0]="Password must be 6 characters. ";
error_msg[1]="Password cannot contain a space. ";
error_msg[2]="One letter must be upper case. ";
error_msg[3]="Passwords do not match. ";
}
void displayMsg(){
cout<<error_msg[0];
cout<<error_msg[1];
cout<<error_msg[2];
}
bool checkLength(){
if(entry.size()<6){
cout<<error_msg[0]<<endl;
return true;
}
return false;
}
bool checkSpaces(){
int i;
for(i=0;i<entry.size();i++){
if(entry[i]==' '){
cout<<error_msg[1]<<endl;
return true;
}
}
return false;
}
bool checkUpper(){
int i;
for(i=0;i<entry.size();i++){
if(entry[i]>='A'&&entry[i]<='Z'){
return false;
}
}
cout<<error_msg[2]<<endl;
return true;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.