Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to call this bool function which has two const variables?
I have tried different ways by changing the order of the variables but nothing seems to work.

What I have tried:

#include <iostream>
#include <cctype>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;

void displayMenu();
bool verify(const char, const int);


int main(){

	displayMenu();
	const int SIZE = 30;
	char pwd[SIZE];
	cout << "\n\nEnter your password: ";
	cin.ignore();
	cin.getline(pwd, SIZE);

	verify(pwd[SIZE], SIZE);

	//terminate
	return 0;
}

void displayMenu()
{
	const char FILL  = '+';
	const int  WIDTH = 60;
	cout << setfill(FILL) << setw(WIDTH) << "" << endl;
	cout << "Welcome to my password verifier and strength score program!\n";
	cout << "You password must meet all of the following conditions: \n";
	cout << "\t1. Contain at least twelve characters\n";
	cout << "\t2. Contain at least one lowercase letter\n";
	cout << "\t3. Contain at least one uppercase letter\n";
	cout << "\t4. Contain at least one digit\n";
	cout << "\t5. Contain at least one punctuation mark\n";
	cout << "\t6. Cannot contain any whitespace\n";
	cout << setfill(FILL) << setw(WIDTH) << "" << endl;

}
bool verify(const char pwd[], const int SIZE)
{
	for (unsigned i = 0; i <strlen(pwd); i++){
		 return (isdigit(pwd[i]));

	}
	return true;
}
Posted
Updated 18-Nov-22 4:51am

[update]
OOOPS, I overlooked it (thanks, Griff)

The function declaration must (of course) match its definition, so you need to declare verify, the way Griff pointed out:
C++
bool verify(const char pwd[], const int SIZE);
[/update]


Roughly speaking, the const on function parameters are just a promise that passed values are not changed by the function itself. Hence the following code would work fine
C++
int main()
{
  char password[] = "fooboogoo";
  cout << "verify(password) " << verify(password, sizeof(password)) << "\n";
}

That said, your verify function is incomplete and possibly wrong.
 
Share this answer
 
v2
In C++, arrays use zero based indexes: so the only valid indexes for an array with 3 elements are 0, 1, and 2.
Your code:
	char pwd[SIZE];
...
	verify(pwd[SIZE], SIZE);
Tries to pass the fourth element, if SIZE was 3.

What you need to do is change the forward reference to match the actual declaration, and pass the pointer to the first element:
bool verify(const char[], const int);


int main(){
...
	char pwd[SIZE];
...
	verify(pwd, SIZE);
...
}

bool verify(const char pwd[], const int SIZE)
 
Share this answer
 
change the signature of the function to
C++
bool verify(const char *, const int SIZE)
and you should be done.
 
Share this answer
 
Also note that the const modifier to the integer parameter is unnecessary. Since it is passed by value, the original variable can not be modified anyway since a copy of it was pushed onto the stack. The character pointer parameter requires the const modifier since the pointer is passed to the function so the data it points to could possibly be modified but making that item constant will prevent that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900