In: Computer Science
Develop a program in C++, using functions, to validate a userID. Valid userID specifications:
• 5 - 10 characters long.
• must begin with a letter.
• must contain at least one upper case letter.
• must contain at least one lower case letter.
• must contain at least one decimal digit.
• must contain at least one of the following special characters: #_$
• must not contain any other characters than those specified above.
The main program should loop, inputting userIDs and outputting the userID followed by "valid" or "not valid" as appropriate until the user enters "END". At that point the program ends. You may only use the following system include files: You should develop a comprehensive test set of userIDs. Include this test set in a block comment at the end of your main program. I.e., in the main.cpp file. Write two functions as described below. These should be properly entered into the two files: ID_Functions.h and ID_Functions.cpp. I.e., two functions go into one file. Do not use a "using namespace" in the header, .h, file. Instead, use explicit namespace resolution as needed. E.g., you will have to use std::string to declare a string.
Please look at my code and in case of indentation issues check the screenshots.
---------------ID_Functions.h----------------
//functio declarations
bool containsAnyOf(std::string anyOf, std::string testStr);
bool IsValidID(std::string testStr);
---------------ID_Functions.cpp----------------
#include <iostream>
#include "ID_Functions.h"
bool containsAnyOf(std::string anyOf, std::string
testStr){ //returns true if any char in anyOf is
present in testStr
size_t found;
for(int i = 0; i < anyOf.length();
i++){ //loop through the length of anyOf
found =
testStr.find(anyOf[i]);
//checks if ith char of anyOf is found in
testStr
if (found !=
std::string::npos)
//if found return true
return
true;
}
return false;
}
bool IsValidID(std::string testStr){
if (testStr.length() < 5 || testStr.length() >
10) //string size 5-10 chars check
return false;
char ch = testStr[0];
if (!(('a' <= ch && ch <= 'z') || ('A'
<= ch && ch <= 'Z'))) //must begin with a
letter.
return false;
if(!containsAnyOf("abcdefghijklmnopqrstuvwxyz",
testStr)) //must contain at least one upper case letter.
return false;
if(!containsAnyOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
testStr)) //must contain at least one upper case letter.
return false;
if(!containsAnyOf("0123456789",
testStr)) //must contain at least one decima
digit.
return false;
if(!containsAnyOf("#_$", testStr))
//must contain at least one
of #_$
return false;
for(int i = 0; i < testStr.length();
i++){ //check if any char is not from above
characters
ch = testStr[i];
if(!(('a' <= ch && ch
<= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <=
ch && ch <= '9') || ch == '#' || ch == '_' || ch ==
'$'))
return
false;
}
return true; //if all conditions are met,
return false
}
---------------main.cpp----------------
#include <iostream>
#include "ID_Functions.h"
int main()
{
std::string inp;
std::cout << "Enter userID: ";
std::cin >> inp;
//read
input string
while(inp != "END"){
//if it is not END
if(IsValidID(inp))
//check if
valid
std::cout
<< inp << " valid" << std::endl;
else
std::cout
<< inp << " not valid" << std::endl;
std::cout << "Enter userID:
"; //read next string
std::cin >> inp;
}
return 0;
}
--------------Screenshots-------------------
------------------Output-----------------
----------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou