Comparing Data from TextFile.

I'm currently trying to program a code to run a login system. Which contains
1) Current Username
2) Current Password
3) New Username
5) New Password

there after the data will be saved into a textfile. I'm currently stuck in trying to get program to compare both username and password if both are true. User will then "enter" otherwise "try again".

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
fstream readFile("Login.txt");
using namespace std;
int main()
{
	string username, password,_username,_password;
	string line= " ";
	int choice;
	bool found = false;

	fstream file("Login.txt", ios::in|ios::out|ios::app);//app to create file
	if(!file)
		cout<<"Error"<<endl;
	else
	cout<<" - - - - - - - -Welcome to YOUR TRAVEL PAL- - - - - - - - "<<endl;
	cout<<"Press 1 to Register or 2 to Login";
	cin>>choice;

	if (choice==1)
{
	cout<<"Enter New Username: ";
	cin>>username;
	file<<username<<endl;

	cout<<"Enter New Password: ";
	cin>>password;
	file<<password<<endl;
}
	else if (choice ==2)
{
	fstream inFile; inFile.open("Login.txt");
	
	cout<<"Enter Username: ";
	cin>>username; 
	cout<<"Enter Password: ";
	cin>>password;

	stringstream iss(line);
	iss>>_username>>_password;

	while (getline(readFile,line))
{

	if(username==_username && password==_password)
	{
		cout<<"Login Successfully!"<<endl;
		found=true;
	}

	else 
		cout<<"Invalid Username and Password"<<endl;

}
}
system ("pause");
return 0;
}
Line 41: line is initialized to " " at line 10. You then try to parse _username and _password from line. You don't want to parse line until after you've read something into it.

Move line 42 to line 46.

Line 54: You don't want to display "Invalid username and password" until after you've read the entire file.

Delete line 54.
Add after line 56:
1
2
  if (! found)
    cout<<"Invalid Username and Password"<<endl;


Last edited on
Topic archived. No new replies allowed.