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.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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 ¤tpass, int &sizep){
for(int c = 0; c < sizep; c++){
if (currentpass == commList[c]){
returntrue;
}
}
returnfalse;
}
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.
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 ¤tpass, 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.
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
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...!
{}
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.
#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)
returntrue;
returnfalse;
}
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);
constauto 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);
constauto 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: