Compare cout and text files in strings

Oct 10, 2020 at 7:38am
Hello guys im new here. I need to write some code which compare my password with passwords stored it file Dictionaries.txt if string are the same cout bad pass if not cout good pass
here my bad code
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
  #include <iostream>
#include <fstream>
#include <cstring>


void Passcheck ()
{

         std::string storedPassword, userEntered;
         fstream fin,fout;
         ifstream myfile("Dictionaries.txt");
         std::cout << "Enter the password: ";
         std::cin >> userEntered;
        if(userEntered == storedPassword) {
              std::cout << "\nThat is the bad password!" << std::endl;
         } else {
              std::cout << "That is good password ." << std::endl;
         }
}


int main()
{

 Passcheck();


    return 0;
}

Last edited on Oct 10, 2020 at 7:41am
Oct 10, 2020 at 7:55am
Step 1, read the words in the file.
1
2
3
while ( myfile >>storedPassword ) {
  // do something
}


Step 2, compare with your input
1
2
3
4
5
while ( myfile >>storedPassword ) {
  if(userEntered == storedPassword) {
      // do something
  }
}


Step 3, probably you want to exit the loop if a match is found
1
2
3
4
5
6
7
bool passOk = true;
while ( myfile >>storedPassword ) {
  if(userEntered == storedPassword) {
      passOk = false;
      break;
  }
}


Oct 10, 2020 at 8:26am
Tnx for help Salem c !!!
Oct 10, 2020 at 8:41am
Surely you want the state for passOk reversed? false if not found and true if found?
Topic archived. No new replies allowed.