Reputation: 11
void Manager::ManagerView1()
{
system("cls");
string Ipass;
string Opass;
ifstream Password("Password.txt", ios::in);
if (!Password)
{
cerr << "Check File Integrity";
}
else
{
while (!Password.eof())
{
getline(Password,Ipass);
cout << "Enter Password :\n";
cin >> Opass;
if (Opass == Ipass)
{
cout << "Passwords Match";
}
}
}
}
Text inside the "Password.txt":
Abhik Ray 1123
The password is being read properly, I have already checked that.
When I run the code it lets me enter the password but then the Passwords match doesn't show up as it should. And then it again asks for the password where I am unable to enter it. And then it just quits.
What should I change?
Upvotes: 0
Views: 39
Reputation: 92231
You have several problems, like trying to match only one line from the password file at a time.
The reason for the message is that if (Opass == Ipass)
compares the addresses of the character arrays, not their contents.
If you had used std::string
to store the strings, the comparison would have worked, but with C style strings you need to use if(strcmp(Opass, Ipass) == 0)
.
You might also want to check this question for how to terminate the loop:
Why is iostream::eof inside a loop condition considered wrong?
In the new version of the code with cin >> Opass;
the >>
will only read one word at a time (it stops at each space). So if you type Abhik Ray 1123
you will only get Abhik
in Opass
, and the rest of the line will remain in the input buffer until the next read.
That's also why it doesn't ask for the next input, it just reads the following words that are already there.
To read a whole line of input, you need to use getline(cin, Opass);
, just like when you read from the textfile.
Upvotes: 2
Reputation: 275
Opass and Ipass are pointers and by doing Opass == Ipass
you check if they point to the same area in the memory.
You have to use strcmp
to compare their values.
Upvotes: 0