Here is my text file (sample.txt) format
john 123
jim 89
britney 852
When I read from a file and output the details, it display the last line twice. The program needs to take user name and password separately. So then comparison will be easy.
This is my sample output for the above file
john 123
jim 89
britney 852
britney 852
How can I overcome this and also give me the reason it shows twice in the output.
thanks in advance.
The code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string passwd;
string usern;
string password;
string username;
ifstream infile;
string line;
infile.open("sample.txt");
if(!infile)
{
cerr << "error during opening of the file" << endl;
}
else
{
while (!infile.eof())
{
infile >> usern >> passwd ;
cout << usern << " " << passwd << endl;
}
}
cout << "Enter user name : ";
cin >> username;
if (usern == username)
{
cout << "already exist" << endl;
}
infile.close();
return 0;
}