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.
Code:
ID_Functions.h:
bool hasAny(std::string str1, std::string str2);
bool CheckValidID(std::string str2);
ID_Functions.cpp:
#include <iostream>
#include "ID_Functions.h"
bool hasAny(std::string str1, std::string str2){
size_t found;
for(int i = 0; i < str1.length(); i++){
found = str2.find(str1[i]);
if (found != std::string::npos)
return true;
}
return false;
}
bool CheckValidID(std::string str2){
if (str2.length() < 5 || str2.length() > 10)
return false;
char ch = str2[0];
if (!(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')))
return false;
if(!hasAny("abcdefghijklmnopqrstuvwxyz", str2))
return false;
if(!hasAny("ABCDEFGHIJKLMNOPQRSTUVWXYZ", str2))
return false;
if(!hasAny("0123456789", str2))
return false;
if(!hasAny("#_$", str2))
return false;
for(int i = 0; i < str2.length(); i++){
ch = str2[i];
if(!(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') || ch == '#' || ch == '_' || ch == '$'))
return false;
}
return true;
}
testvalidid.cpp:
#include <iostream>
#include "ID_Functions.h"
int main()
{
std::string userInput;
std::cout << "Enter User ID: ";
std::cin >> userInput;
while(userInput != "END"){
if(CheckValidID(userInput))
std::cout << userInput << " is valid." << std::endl;
else
std::cout << userInput << " is not valid." << std::endl;
std::cout << "\nEnter userID: ";
std::cin >> userInput;
}
return 0;
}
Output: