When I run the following code I randomly get true when it should be false. // Te
ID: 3646424 • Letter: W
Question
When I run the following code I randomly get true when it should be false.// Test to see if there is a special character
int counter = 0;
for (int i = 0; i < size; i++)
{
if (ispunct(password[i]))
{
counter ++;
}
}
cout << counter << endl;
if (counter > 2)
{
cout << "YES Contains a special character" << endl;
status ++;
}
else
{
cout << "NO Contains a special character" << endl;
status = 0;
}
Entire program
#include <iostream>
#include <locale>
#include <cstring>
using namespace std;
bool testPass(char[], int);
int main()
{
int SIZE = 16;
char pass[SIZE];
char pass2[SIZE];
int count = 0;
const int CHOICE_ONE = 1,
CHOICE_TWO = 2,
ANSWER_ONE = 1,
ANSWER_TWO = 2;
int choice, answer;
do
{
cout << "Welcome to the password program. "
<< "You will be asked to enter a password, "
<< "but first, please read the password criteria "
<< "1) Must be 8 - 15 characters long "
<< "2) Must start with a letter or number "
<< "3) Must contain at least one special character "
" (printable character other than a digit, letter, or space) "
<< "4) Must contain at least one digit "
<< "5) Must not contain any whitespace characters "
<< " Please enter a password:" << endl;
cin.getline(pass, SIZE);
while (testPass(pass, SIZE) != true)
{
cout << " I'm sorry, that password does not meet the criteria " << endl;
cout << "1. Enter a different password "
<< "2. Exit the program "
<< "Enter your choice: ";
cin >> choice;
if (choice != CHOICE_TWO)
{
cout << " Please enter a different password:" << endl;
cin.get();
cin.getline(pass, SIZE);
}
else
{
return 0;
}
}
cout << " Congratulations! That is a valid password " << endl;
for (int b = 0; b < 3; b++)
{
cout << "Please re-enter your password for verification" << endl;
cin.getline(pass2, SIZE);
if (strcmp(pass, pass2) == 0)
{
cout << "The passwords match!" << endl;
return 0;
}
else
{
cout << " I'm sorry; you're passwords do not match" << endl;
}
}
cout << " I'm sorry, that password failed to match the first one 3 times in a row " << endl;
cout << "1. Start over with a different password "
<< "2. Exit the program "
<< "Enter your choice: ";
cin >> answer;
}while(answer == ANSWER_ONE);
return 0;
}
bool testPass(char password[], int size)
{
int status = 0;
int count;
// Test to see if password is 8-15 characters long
int length;
length = strlen(password);
if (length >=8 && length <= 15)
{
cout << "YES Between 8 and 15 characters long" << endl;
status ++;
}
else
{
cout << "NO Between 8 and 15 characters long" << endl;
status = 0;
}
// Test to see if first character is a letter/number or not
if (isalnum(password[0]))
{
cout << "YES Begins with a letter or number" << endl;
status ++;
}
else
{
cout << "No Begins with a letter or number" << endl;
status = 0;
}
// Test to see if there is a special character
int counter = 0;
for (int i = 0; i < size; i++)
{
if (ispunct(password[i]))
{
counter ++;
}
}
cout << counter << endl;
if (counter > 2)
{
cout << "YES Contains a special character" << endl;
status ++;
}
else
{
cout << "NO Contains a special character" << endl;
status = 0;
}
// Test to see if at least one digit is present
int count1 = 0;
for (int j = 0; j < size; j++)
{
if (isdigit(password[j]))
{
count1++;
}
}
if (count1 > 0)
{
cout << "YES Contains atleast one digit" << endl;
status ++;
}
else
{
cout << "NO Contains atleast one digit" << endl;
status = 0;
}
// Test to see if password contains whitespace
int count2 = 0;
for (int k = 0; k < size; k++)
{
if (isspace(password[k]))
{
count2++;
}
}
if (count2 == 0)
{
cout << "YES Contains no whitespace characters" << endl;
status ++;
}
else
{
cout << "NO Contains no whitespace characters" << endl;
status = 0;
}
if (status == 5)
return true;
else
return false;
}
Explanation / Answer
I found the problem The if statement is wrong It should be: "if ( counter > 0) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.