how to finish this program #include <cstring> using namespace std; //function pr
ID: 3704458 • Letter: H
Question
how to finish this program
#include <cstring>
using namespace std;
//function prototypes
bool testPassWord(char[]);
int countLetters(char*);
int countDigits(char*);
int main()
{
char passWord[20];
cout << "Enter a password consisting of exactly 5 letters and 3 digits:" << endl;
cin.getline(passWord,20);
if (testPassWord(passWord))
cout << "Please wait - your password is being verified" << endl;
else
{
cout << "Invalid password. Please enter a password with exactly 5 letters and 3 digits" << endl;
cout << "For example, my37RuN9 is valid" << endl;
}
// Fill in the code that will call countLetters and
// countDigits and will print to the screen both the number of
// letters and digits contained in the password.
return 0;
}
//**************************************************************
// testPassWord
//
// task: determines if the word in the
// character array passed to it, contains
// exactly 5 letters and 3 digits.
// Precondition: a word contained in a character array
// Postcondition: true if the word contains 5 letters & 3
// digits, false otherwise
//
//**************************************************************
bool testPassWord(char custPass[])
{
int numLetters, numDigits, length;
length = strlen(custPass);
numLetters = countLetters(custPass);
numDigits = countDigits(custPass);
if (numLetters == 5 && numDigits == 3 && length == 8 )
return true;
else
return false;
}
// the next 2 functions are from Sample Program 10.5
//**************************************************************
// countLetters
//
// task: counts the number of letters (both
// capital and lower case)in the string
// Precondition: a string
// Postcondition: the number of letters in the string
//
//**************************************************************
int countLetters(char *strPtr)
{
int occurs = 0;
while(*strPtr != '')
{
if (isalpha(*strPtr))
occurs++;
strPtr++;
}
return occurs;
}
//**************************************************************
// countDigits
//
// task: counts the number of digits in the string
// data in: a string
// data returned: the number of digits in the string
//
//**************************************************************
int countDigits(char *strPtr)
{
int occurs = 0;
while(*strPtr != '')
{
if (isdigit(*strPtr)) // isdigit determines if the character is a digit
occurs++;strPtr++;
}
return occurs;
}
Explanation / Answer
Hello. The code already looks finished (or almost finished). I have written the code to print the number of letters and digits in the password once a password has been entered; just what mentioned in the comments. If you are looking for something else, you have to specify it, drop a comment. Thanks.
This is the completed code.
//code.cpp
#include <cstring>
#include<iostream>
using namespace std;
//function prototypes
bool testPassWord(char[]);
int countLetters(char*);
int countDigits(char*);
int main()
{
char passWord[20];
cout << "Enter a password consisting of exactly 5 letters and 3 digits:" << endl;
cin.getline(passWord,20);
if (testPassWord(passWord))
cout << "Please wait - your password is being verified" << endl;
else
{
cout << "Invalid password. Please enter a password with exactly 5 letters and 3 digits" << endl;
cout << "For example, my37RuN9 is valid" << endl;
}
int numLetters=countLetters(passWord);
int numDigits=countDigits(passWord);
cout<<"The password contain "<<numLetters<<" letters and "<<numDigits<<" digits..."<<endl;
// Fill in the code that will call countLetters and
// countDigits and will print to the screen both the number of
// letters and digits contained in the password.
return 0;
}
//**************************************************************
// testPassWord
//
// task: determines if the word in the
// character array passed to it, contains
// exactly 5 letters and 3 digits.
// Precondition: a word contained in a character array
// Postcondition: true if the word contains 5 letters & 3
// digits, false otherwise
//
//**************************************************************
bool testPassWord(char custPass[])
{
int numLetters, numDigits, length;
length = strlen(custPass);
numLetters = countLetters(custPass);
numDigits = countDigits(custPass);
if (numLetters == 5 && numDigits == 3 && length == 8 )
return true;
else
return false;
}
// the next 2 functions are from Sample Program 10.5
//**************************************************************
// countLetters
//
// task: counts the number of letters (both
// capital and lower case)in the string
// Precondition: a string
// Postcondition: the number of letters in the string
//
//**************************************************************
int countLetters(char *strPtr)
{
int occurs = 0;
while(*strPtr != '')
{
if (isalpha(*strPtr))
occurs++;
strPtr++;
}
return occurs;
}
//**************************************************************
// countDigits
//
// task: counts the number of digits in the string
// data in: a string
// data returned: the number of digits in the string
//
//**************************************************************
int countDigits(char *strPtr)
{
int occurs = 0;
while(*strPtr != '')
{
if (isdigit(*strPtr)) // isdigit determines if the character is a digit
occurs++;strPtr++;
}
return occurs;
}
/*OUTPUT1*/
Enter a password consisting of exactly 5 letters and 3 digits:
abcd123x
Please wait - your password is being verified
The password contain 5 letters and 3 digits...
/*OUTPUT2*/
Enter a password consisting of exactly 5 letters and 3 digits:
xyz12
Invalid password. Please enter a password with exactly 5 letters and 3 digits
For example, my37RuN9 is valid
The password contain 3 letters and 2 digits...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.