Worked more on my password checker but still need some help

So I did some more work on this, and my logic is that each of my passwords I will check against the list of 100 passwords but im having some trouble with the functions, im getting errors like "expected primary expression" and I dont know what that means. I think I have my prototypes wrong, but I cant find anything online for examples of function prototypes when passing by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

bool comparepasswords(&string[], &string, &int);

int main(){
    
    //define ALL variables
    int passed, failed, sizeP, sizeC;
    
    string *commList, *passList, passName, commName, numPass, numCheck, temp;
    
    //prompt for names of files that will be opened
    cout << "Please enter the name of the file: ";
    cin >> passName;
    
    cout << "Please enter the name of the common passwords file: ";
    cin >> commName;
    
    
    //open files and check if they are open
    ifstream passwords;
    passwords.open(passName);
    if(!passwords){
        cout << "Error opening file" << endl;
        return 1;
    }
    
    ifstream commPass;
    commPass.open(commName);
    if(!commPass){
      cout << "Error opening file" << endl;
      return 1;
    }
    
    //get the number of passwords by seing the first value of the array
    getline(passwords, numPass);
    
    //convert it to a int
    sizeP = stoi(numPass);
    
    // make it the size of a new array
    passList = new string[sizeP];
    
    //Do the same for the other array of common passwords
    getline(commPass, numCheck);
    
    sizeC = stoi(numCheck);
    
    commList = new string[sizeC];
    
    cout << "Checked " << sizeP << " passwords in file: " << passName << endl;
    
    //enter both of these text files into the arrays
    for(int count = 0; count < sizeC; count++){
      getline(commPass, commList[count]);
    };
    
    for(int count = 0; count < sizeP; count++){
      getline(passwords, passList[count]);
      
      
      comparepasswords (passList[], temp, sizeP);
      if (comparepasswords = true)
        passed += 1;
      else
        failed += 1;
    };
    
    cout << "Passed: " << passed << endl;
    cout << "Failed: " << failed << endl;
}

bool comparepasswords (string &commList[], string &currentpass, int &sizep){
  for(int c = 0; c < sizep; c++){
    if (currentpass == commList[c]){
      return true;
    }
  }
  return false;
}
Last edited on
//here is the first list of passwords that I will be checking
50
ahBij1zi
ye4ohXohz
eichooli2V
niel6Taec
%hcie8yae9
za6phaeR
va6%g0ei
ieF9looz
Veet4aye
gie7MaeTho
aiGhahgh7ee
Raic3mos
EiGh0SeeW
Koo6haiQu
%hm2ooVu
ohkeeCh6I
ibu5miiFoh
FahL7uYu
haedah8H
An1eizav
Nee5ooz3!
eghe0Eecho
tabi1Aiwe
aph9EiY8a
ahTohTh8go
iip1Keiqu
shadoW123!
yoBaiw0u
lahm7cu6Lai
paeteeNu1d
1234qwer
iegae6ee^^
HahKa9ethu
Aex3eexam
pooke2Ch
aingi1ohTh7
Phah9Aeko
Shahgh3%hso
yen9pha0Ei
ohxei6Iebe
aaxeeNa3shi
^^fe9seiYa
ung0eiBooch
Aic6oong
%ja9jae1
%hd9ahra
WooTh5za
chahl0ieGa
joQueigh0pa
aipoh6Aeyo^^
//Here is the list of common passwords ill be checking against.
100
ABCabc123456!
Password1
12345678
1Qwerty!
&Abc123456789
12345ZZz%
1234ABc123
111111
1234567
dragoN11!
123123
Baseball1!
%Abc123
Football
monkey
1!Letmein
Wildcat^1
shadoW123!
master
666666aaA!
qwertyUIOP!1
AAa%123321
!1Mustang
1234567890
michael
654321
william
superman
1qaz2wsX!
7777777
1234qwer
zzZ121212%
A000000aaa!
123qazwsX
123qwe
killer
Trustno1!
jorDan%
%jennifer99
zxcvbnm
asdfgh
hunter
buster
123Soccer!
harley
batman
andrew
Zz!tigger
sunshine
iloveyou
gfhjkm
2000
!!1Charlie
robert
thomas
HockeY99%
ranger
daniel
Starwars!123
abcKlaster1
112233ABc1!
george
secret
computer
michellE%9
Jessica11^
^Pepper2
1111
zxcvbn
555555
11111111
131313Qwerty1!
!!Freedom123
777777
pass
^DiamonD^
Maggie555%
abc159753!
Aaaaaa&1
88Ginger^
princeSS%1
joshua
cheese
AmandA%%1
ABCsummer&&
Love%123123
AshleyAshley321!
%Corvette1
nicole
Chelsea&&
Hello123!
321%Matthew
Access888!
YankeesYY&&
aaaZZZ987654321!
!!Dallas11
Austin11!!
Thunder!999
Taylor^1
maTrix987!
bool comparepasswords(&string[], &string, &int);
what do you think these arguments are here? & on the front means address of .. a pointer of sorts, but not like this... syntax is no good.

try
bool comparepasswords(string[], string&, int&);

and, even better, consider going ahead and naming them for readability/intent:

bool comparepasswords(string list[], string &pwd, int &count);

and on down at 65 you have
comparepasswords (passList[], temp, sizeP);
should just be
comparepasswords (passList, temp, sizeP);

and then it gets a bit more exciting.
if (comparepasswords = true) //this is all kinds of bad.
//first, its a function, second it says assign the function to true, as = is assign, and == is compare. legal if(x = y) for ints WTILL COMPILE AND RUN but it is NOT checking x and y for equality, it ASSIGNS X THE VALUE OF Y THEN COMPARES X AGAINST ZERO!!!

what you wanted was:
bool result = comparepasswords (passList, temp, sizeP); //create a new bool and hold the answer...
if(result)
{...}
or, if you want,
if( comparepasswords (passList, temp, sizeP) ) //is this function return true?
{...}
I can't recommend enough putting {} even on one line blocks like your if/else here. Eventually you will understand why it helps, once you debug something with a bad indent that LOOKS like its in a block but isnt.

you now increment passed or failed, which are not initialized, so random + 1 is random.

and finally another syntax garble on 77:
make it
bool comparepasswords (string commList[], string &currentpass, int &sizep){

and the program now runs for me.
Basically, you are having syntax problems, so you need to review your basics. That is a good place to be, as this code is a little complicated and c++ syntax for references and pointers and arrays can be a little weird to get settled in one's mind. Keep it up, this is a good try.... just watch those &s and note that & means multiple things. IN GENERAL:
something = &variable //address of, this is a pointer
or foo(&variable) //pointer
or, in short, when being consumed, & on the left is a pointer/address of.

while
int &x = y; //reference, x is being created not consumed.
void foo(int &x); //reference, x is defined, not consumed, here
and so on.
Last edited on
@jonnin cant tell you how much of a lifesaver you are, one more issue, when I return from my bool function, it doesnt let me say
1
2
3
if (function == true){
do something;
}

how would I go about dealing with a bool return like this?

will the compiler just assume its true if I do something like:
1
2
3
if(funciton){
cout << "hello wlrd";
}
Last edited on
Ok, so I tried doing that and got:
Please enter the name of the file: test.txt
Please enter the name of the common passwords file: commonpass.txt
Checked 50 passwords in file: test.txt
Passed: 50
Failed: 32764

so something definately went wrong...
I finished the above post. Go re-read it. It was in progress before.
specifically, the uninitialized variables are causing this.

for your example, yes, that is not ASSUMED, it is well defined c++ syntax.
c++ bool is defined as true (1) and false (0).
it is understood that values that can be 0 or 1 can be used as booleans, such as integers following the logic if it is zero it is false, else it is true. For example: int x = 42; //x is true, for it is not zero.

functions that return bools, ints, chars, doubles, and similar 0 or 1 possible things can be evaluated as boolean, and I gave your code an example of that above.

your example... what is function? a function name collapses into a pointer which is a special integer... this may work, I think it probably does, but its not gonna do what you want.
if(funciton){
cout << "hello wlrd";
}

typically you see
int foo()
...
if(foo()) //see the ()? that is critical. if(foo) and if(foo()) are very, very different things. one checks a pointer against nullptr (zero) and one checks the result of the function...!
{}
Last edited on
I do have passed and failed initialized on line 11, also I redid some of the other stuff you said and its saying that only one passes and 49 fail, which definately seems very wrong. How would you recomend fixing errors when your program returns 0? Like it doesnt tell you which line the error is on... do you have any suggestions.
now you debug it. you either use a debugger or add print statements to see what went wrong and where. Debuggers are powerful, but can take a bit to learn if you haven't yet. Visual studio has a great one built in.
thanks so much for your help!
What happened to the code suggested in your other post
https://cplusplus.com/forum/beginner/284950/

If you need a password compare function then consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <fstream>

bool comparepasswords(const std::string* comm, const std::string& pass, size_t sze) {
	for (size_t i {}; i < sze; ++i)
		if (comm[i] == pass)
			return true;

	return false;
}

std::string* readFile(const std::string& nam, size_t& sze) {
	std::string* list {};
	sze = 0;

	if (std::ifstream ifs { nam })
		if ((ifs >> sze >> std::ws) && sze > 0) {
			list = new std::string[sze];
			//std::cout << sze << " passwords in file: " << nam << '\n';

			size_t cnt {};

			while (cnt < sze && getline(ifs, list[cnt++]));

			if (cnt != sze) {
				delete[] list;
				list = nullptr;
				sze = 0;
			}
		}

	return list;
}

int main() {
	std::string passName;
	size_t sizeP {};

	std::cout << "Please enter the name of the file: ";
	std::getline(std::cin, passName);

	const auto passList { readFile(passName, sizeP) };

	if (sizeP) {
		std::string commName;
		size_t sizeC {};

		std::cout << "Please enter the name of the common passwords file: ";
		std::getline(std::cin, commName);

		const auto commList { readFile(commName, sizeC) };

		if (sizeC) {
			size_t good {};

			for (size_t p {}; p < sizeP; ++p)
				good += comparepasswords(commList, passList[p], sizeC);

			std::cout << "Passed: " << good << '\n';
			std::cout << "Failed: " << sizeP - good << '\n';
			delete[] commList;
		} else
			std::cout << "Cannot open common password file\n";
	} else
		std::cout << "Cannot open password file\n";

	delete[] passList;
}



Please enter the name of the file: passname.txt
Please enter the name of the common passwords file: commname.txt
Passed: 2
Failed: 48


using your supplied file contents.

As well as comments above, your last posted code also had potential memory issues as the allocated memory isn't deleted after use. it's good practice not to rely on the OS doing this for you.

Also, in the posted code what is temp? You're trying to do the password compare within the loop to read from the password file. That is perfectly reasonable (as opposed to reading both files first into memory). Doing it that way gives something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <fstream>

bool comparepasswords(const std::string* comm, const std::string& pass, size_t sze) {
	for (size_t i {}; i < sze; ++i)
		if (comm[i] == pass)
			return true;

	return false;
}

size_t readSize(const std::string& nam, std::ifstream& ifs) {
	size_t sze {};

	ifs.close();
	ifs.open(nam);

	ifs >> sze >> std::ws;
	return sze;
}

std::string* readFile(const std::string& nam, size_t& sze) {
	std::string* list {};
	std::ifstream ifs;

	if (sze = readSize(nam, ifs); ifs && (sze > 0)) {
		size_t cnt {};

		for (list = new std::string[sze]; (cnt < sze) && getline(ifs, list[cnt++]););

		if (cnt != sze) {
			delete[] list;
			list = nullptr;
			sze = 0;
		}
	}

	return list;
}

int main() {
	std::string commName;
	size_t sizeC {};

	std::cout << "Please enter the name of the common passwords file: ";
	std::getline(std::cin, commName);

	if (const auto commList { readFile(commName, sizeC) }; commList) {
		std::string passName;
		std::ifstream passif;

		std::cout << "Please enter the name of the file: ";
		std::getline(std::cin, passName);

		if (auto sizeP { readSize(passName, passif) }; passif && (sizeP > 0)) {
			size_t passed {}, failed {};
			std::string password;

			for (size_t p {}; (p < sizeP) && std::getline(passif, password); ++p)
				if (comparepasswords(commList, password, sizeC))
					++passed;
				else
					++failed;

			if (passed + failed != sizeP)
				std::cout << "Expected " << sizeP << " passwords. Read " << passed + failed << '\n';

			std::cout << "Passed: " << passed << '\n';
			std::cout << "Failed: " << failed << '\n';
		} else
			std::cout << "Cannot open password file\n";

		delete[] commList;
	} else
		std::cout << "Cannot open common password file\n";
}

Last edited on
Based purely on the given code, then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

bool comparepasswords(const string[], const string&, int);

int main() {
	int passed {}, failed {}, sizeP {}, sizeC {};
	string *commList, passName, commName, numPass, numCheck;

	cout << "Please enter the name of the file: ";
	cin >> passName;

	cout << "Please enter the name of the common passwords file: ";
	cin >> commName;

	ifstream passwords;

	passwords.open(passName);
	if (!passwords) {
		cout << "Error opening file" << endl;
		return 1;
	}

	ifstream commPass;

	commPass.open(commName);
	if (!commPass) {
		cout << "Error opening file" << endl;
		return 1;
	}

	getline(passwords, numPass);
	sizeP = stoi(numPass);
	getline(commPass, numCheck);
	sizeC = stoi(numCheck);
	commList = new string[sizeC];

	cout << "Checked " << sizeP << " passwords in file: " << passName << endl;

	for (int count = 0; count < sizeC; count++)
		getline(commPass, commList[count]);

	for (int count = 0; count < sizeP; count++) {
		string password;

		getline(passwords, password);

		if (comparepasswords(commList, password, sizeC))
			++passed;
		else
			++failed;
	}

	cout << "Passed: " << passed << endl;
	cout << "Failed: " << failed << endl;

	delete[] commList;
}

bool comparepasswords(const string commList[], const string& currentpass, int sizeC) {
	for (int c = 0; c < sizeC; c++) {
		if (currentpass == commList[c]) {
			return true;
		}
	}
	return false;
}

Last edited on
Topic archived. No new replies allowed.