0

I want to create a text file for first run carrying the password and use the code to check the entire string for the password entered previously. The current code returns true value for the 1st few alphabets even if the whole password isn`t entered.

    int Manager::ManagerView1()
   {
    Passwordsearch:
    system("cls");
    string search;
    int offset,ErrorVar;
    string line;
    ifstream Myfile;
    Myfile.open("Password.txt");

    cout << "Enter your Password :\n";
    cin >> search;
    if (Myfile.is_open())
    {
        while (!Myfile.eof())
    {
        getline(Myfile,line);
        if ((offset = line.find(search, 0)) != string::npos)
        {
            cout << "Password Accepted ..\n";
            system("pause");
        }
        else
        {
            cout << "Password Incorrect. \nPress\n 1.) Go back to the main     screen\n 2.) Re-Enter Password \n";
            cin >> ErrorVar;
            if (ErrorVar == 1)
            {
                system("PAUSE");
                return 1;
            }
            else if (ErrorVar == 2)
            {
                system("PAUSE");
                system("cls");
                goto Passwordsearch;
            }
            else
            {
                cout << "Wrong option !! Please try again\n";
                system("PAUSE");
                return 1;
            }
        }
      }
      }
      }

This is the password file that i want to check the string from: enter image description here

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Abhik Ray
  • 11
  • 6
  • Please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Mar 18 '17 at 06:45

2 Answers2

0

Your problem is because of how the find function works. It looks for the sub-string contained in search.

You have to extract the complete password from the file, and then compare using equality instead of just searching for a sub-string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I thank you for the quick reply . I am actually a beginner at c++ so if you could tell me what to do exactly it would be helpful – Abhik Ray Mar 18 '17 at 06:49
  • @AbhikRay No I can't tell you, because I don't know the contents of the file or its format. – Some programmer dude Mar 18 '17 at 06:50
  • Ok so the contents of the file Password.txt is ("Anonymous") and when i entered Anon in the command window it said password accepted – Abhik Ray Mar 18 '17 at 06:51
  • Also how do i change it so that it actually creates the Password.txt file with the string when it`s run for the first time. – Abhik Ray Mar 18 '17 at 06:53
  • @AbhikRay Please edit your question to include a copy-paste (as text) of the contents of a single line. Is the parentheses really in the file? The double-quotes? If so are those part of the password? – Some programmer dude Mar 18 '17 at 06:57
  • I posted a picture of the text file and no the parenthesis and the double quotes are not part of the password. – Abhik Ray Mar 18 '17 at 07:05
  • @AbhikRay Please don't post images of text, just copy-paste the contents *as text*, with proper formatting. – Some programmer dude Mar 18 '17 at 07:10
  • @AbhikRay As for the problem, you have already done the extraction with the `getline` call. Now re-read the second paragraph in my answer. – Some programmer dude Mar 18 '17 at 07:10
  • I did try it but i dont understand how to compare it . Can you please tell me what to edit like specifically what to edit – Abhik Ray Mar 18 '17 at 07:38
  • 'getline' is reading the string from the text file but i am not storing it anywhere also search is the password that i am entering through the keyboard so how do i compare it using the == operator. – Abhik Ray Mar 18 '17 at 07:43
  • @AbhikRay `line == search` – Some programmer dude Mar 18 '17 at 08:29
0

When you use string.find(param1, param2) it just tells you if a string in this case "Anonymous" does contain text you put as a first paramater inside find() function.

If you want to avoid that you shouldn't be using *find()* at all. Instead, you should do comparing.

First you read the string from txt file and store it in a string line. Then you ask user to enter password after he enters password, you check if your string line is eqaul to password .

std::string my_pass = "Anonymous";//Get it from file.
std::string input_pass = "";
std::cin >> input_pass;

if(input_pass==my_pass){
    std::cout << "PASSWORD CORRECT\n";
}
lowarago
  • 88
  • 8