Please help me review my program !! Below I copied what I am suppose to do. I al
ID: 3552189 • Letter: P
Question
Please help me review my program !! Below I copied what I am suppose to do. I also copied my program at the very bottom which is not compling .
. Each line will contain a password that must meet the following criteria:
This process will continue until the user enters a line beginning with an exclamation point ('!'). Once the user is done entering password information, the following should be displayed:
Processng
So the program will read characters - one at a time - from the keyboard. Since the program will quit when the first char in a line is a '!' - but will output a line whenever a newline character is encountered, the following logic can be used (NOTE: ch is a char variable):
This function will determine if the character ch is a digit '0' through '9'. If the character is a digit, return 1. Otherwise, return 0.
DO NOT use the isdigit function from the <cctype> library. You are writing your own version of the function.
This function will determine if the character ch is an uppercase 'A' through 'Z' character. If the character is uppercase, return 1. Otherwise, return 0.
DO NOT use the isupper function from the <cctype> library. You are writing your own version of the function.
This function will determine if the character ch is a lowercase 'a' through 'z' character. If the character is lowercase, return 1. Otherwise, return 0.
DO NOT use the islower function from the <cctype> library
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>
using namespace std;
const char password[]={'T','c','c','d','0','3'};
bool verifypass(char *userinput,bool valid) //password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
int length;
length = strlen(userinput); //checks for number of characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// this section is to verify uppercase charaters in userinput string
{
bool upper = false;
const int l = strlen(userinput);
for(int i = 0; i < l; ++i)
{
upper = (isupper(userinput[i]) != 0);
if(upper)
break;
}
bool valid = upper;
if(!upper)
cout << "The password must have at least one upper case character." << endl;
}
// Below wants to test the userinput for any digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"Passwords must include at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
verify_chars(userinput,valid);// fuction designed to test the number of characters
verifypass(userinput,valid); // Funtion designed to strcmp the password and userinput
return 0;
Explanation / Answer
#include <iostream>
using namespace std;
int isDigit(char ch)
{
if(ch>='0'&&ch<='9')
return 1;
return 0;
}
int isUpper(char ch)
{
if(ch>='A'&&ch<='Z')
return 1;
return 0;
}
int isLower(char ch)
{
if(ch>='a'&&ch<='z')
return 1;
return 0;
}
int checkPassword(int digitflag,int upperFlag,int lowerFlag,int length)
{
if(length>=6&&digitflag&&upperFlag&&lowerFlag)
{
cout<<"Entered Password Full Fill All Requirement So Valid."<<endl;
return 1;
}
else
{
cout<<"Entered password is invalid Because"<<endl;
if(length<6)
cout<<"It is not at least 6 length"<<endl;
if(!digitflag)
cout<<"It does not include any digit."<<endl;
if(!upperFlag)
cout<<"It does not have any upper case char"<<endl;
if(!lowerFlag)
cout<<"it does not inlude any lower case char"<<endl;
return 0;
}
}
int main()
{
char ch;
cin.get(ch);
int valid=0,invalid=0;
while(ch!='!')
{
int digitflag=0,lowerFlag=0,upperFlag=0;
int charCount=0;;
while(ch!=10)
{
if(isDigit(ch))digitflag=1;
else if(isUpper(ch))upperFlag=1;
else if(isLower(ch))lowerFlag=1;
charCount++;
cin.get(ch);
}
if(checkPassword(digitflag,upperFlag,lowerFlag,charCount))
valid++;
else
invalid++;
cin.get(ch);
}
return 0;
}