Login/Register Problem

I'm trying to make a program that gets input from the user and enters it in the "accinfo".txt, basically a register/login program. but i'm getting problems. When i try to login it says it's the incorrect login info but it's correct. Please tell me what i'm doing wrong or if my logic is wrong, I'm still a beginner started learning 2 days ago.

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
  #include <iostream>
#include <cmath>
#include <iostream>
#include <fstream>
using namespace std;
void login();
void reg();
int main()
{
    system("Color 0A");
    int menu;
    cout << "1. Register" <<endl;
    cout << "2. Login" <<endl;
    cout << ": ";
    cin >> menu;
    if(menu==1){
        reg();
    }else if(menu==2){
        login();
    }

    return 0;
}
void reg(){
    string rname;
    string rpass;
    cout << "Enter your username : ";
    cin >> rname;
    cout << "Enter your password : ";
    cin >> rpass;
    ofstream accinfo;
    accinfo.open("accinfo.txt", ios::app);
    accinfo << rname << " ";
    accinfo << rpass << " ";
    accinfo.close();
}
void login(){
    string lname;
    string lpass;
    string rname,rpass;
    cout << "Enter your username : ";
    cin >> lname;
    cout << "Enter your password : ";
    cin >> lpass;
    ofstream accinfo;
    accinfo.open("accinfo.txt");
    if(lname==rname and lpass==rpass ){
        cout << "Log in Succesful";
    }else if(!(lname==rname && lpass==rpass)){
        cout << "Incorrect Login info." <<endl;
}
    accinfo.close();
}
You declare variables 'rname' and 'rpass' inside your login function, on line 40. By default, std::strings are empty.

Then, you enter information into 'lname' and 'lpass', but rname and rpass remain as empty strings.

In other words, you are not setting rname or rpass to what you think you're setting them to. Try using a debugger and viewing what the value of each string is. Or, print rname/rpass to the screen temporarily for troubleshooting.

Also, the ofstream that you open on line 46 is just overwriting whatever used to be in accinfo.txt, and nothing else.

If you're trying to compare your string against what's saved in your file, you need to open the file for reading.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream fin("accinfo.txt");
if (!fin)
{
    cout << "Error opening file!\n";
    return;
}

if (!(fin >> rname >> rpass))
{
    cout << "Error reading contents of file!\n";
    return;
}

if (lname == rname && lpass == rpass)
{
    // success
}

// ... 
Last edited on
When i try this code, it says that rname and rpass are not declared in this scope, even tho i opened the "accinfo.txt" file for reading. This is the only problem and i don't know how to fix it. this is the current 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
30
31
32
33
34
35
36
37
38
39
40
41
42
void login(){
    string lname;
    string lpass;
    cout << "Enter your username : ";
    cin >> lname;
    cout << "Enter your password : ";
    cin >> lpass;
    ifstream accinfo;
    ifstream fin("accinfo.txt");
    if (!fin)
    {
        cout << "Error opening file!\n";
        return;
    }
    if (!(fin >> lname >> lpass))
    {
        cout << "Error reading contents of file!\n";
        return;
    }
    if (lname == rname && lpass == rpass)
    {
        // success
    }

}

void reg(){
    string rname;
    string rpass;
    cout << "Enter your username : ";
    cin >> rname;
    cout << "Enter your password : ";
    cin >> rpass;
    cout << "Register Succesful." <<endl;
    ofstream accinfo;
    accinfo.open("accinfo.txt", ios::app);
    accinfo << rname << " ";
    accinfo << rpass << " ";
    accinfo.close();
    login();

}
Consider:

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
void login() {

	ifstream fin("accinfo.txt");

	if (!fin) {
		cout << "Error opening file!\n";
		return;
	}

	string lpass, lname;

	cout << "Enter your username : ";
	cin >> lname;

	cout << "Enter your password : ";
	cin >> lpass;

	for (string rname, rpass; fin >> rname >> rpass; )
		if (lname == rname && lpass == rpass) {
			// success
			cout << "Welcome " << lname << '\n';
			return;
		}

	cout << "Username not found\n";
}

void reg()
{
	string rname;
	string rpass;

	cout << "Enter your username : ";
	cin >> rname;
	cout << "Enter your password : ";
	cin >> rpass;

	ofstream accinfo("accinfo.txt", ios::app);

	if (!accinfo) {
		cout << "Error opening file!\n";
		return;
	}

	accinfo << rname << " " << rpass << '\n';
	accinfo.close();
	login();
}


It works!!
Thank you so much. But can you explain a little what exactly did you do?
Basically the rname/rpass used in login() are not those in reg(). login() knows nothing about reg() and its variables. Use one set of variables for the user entered data and one set for the data read from the file.
I understand now, Thanks.
Topic archived. No new replies allowed.