Functions not working as they should

Hi there, I have my code that I have been working on for a week straight and all the research I've done has lead me here to see if I can get some much needed help on this.

Assignment is:
1. Create a program to verify a user name and password given by a user
2. Create a text file named "correctData.txt" (You will NOT upload this file during submission). Place a username on line 1 in the txt file and a password on line 2 in the file. This will be used for testing.
3. Create the following functions and call them starting from main:

void Login () - This function is responsible for displaying the prompts asking for user name and password. It will only allow a user to attempt a password 3 times for security measures before returning back to main. You will need to use a loop to accomplish this task.

bool CheckCredentials (string username, string password)- In order to validate the username and password. This is a boolean return and will return true or false from this function if the data verified does not match the file. 
You will need to use fstream to create an instance of a file using ifstream. Compare the strings to the data in the text file. Remember, the username will be on line 1 in the text file and the password will be on line 2.


Remember:
Close the fstream file before returning 0
Prototype functions.
Don't use global variables

Here is what I have:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 #include <iostream>
#include <string>
#include <fstream>
using namespace std;

void Login(string, string);
bool CheckCredentials(string userName, string passWord);

int main()
{
	ifstream inFile;
	string userName;
	string passWord;
	string u;
	string p;
	
	inFile.open("CorrectData.txt");

	Login(userName, passWord);
	CheckCredentials(userName, passWord);
	
	inFile.close();

	return 0;
}

void Login(string u,string p)
{
	ifstream inFile;
	
	inFile.open("CorrectData.txt");

	cout << "Login" << endl; 
	cout << "\nUsername: ";
	cin >> u;
	cout << "Password: ";
	cin >> p;

	
	for (int count = 0; count <= 3; count++)
	
	if (count ==3)
		{
			cout << "\nYou have " << count << " remaining.\n";
		}

	else
		{
			cout << "\nAttempts exceeded.";
		}

}



	bool CheckCredentials(string userName, string passWord)
{
	bool status;
	string u, p;
	ifstream inFile;
	inFile.open("CorrectData.txt");

	if (userName == u &&  passWord == p)
	{
		status = true; 
		cout << "\nLogin sucessful!" << endl;
	}

	else (!status);
	{
		cout << "\nInvalid username or password. " << endl;
		Login(userName, passWord);

		inFile.close();
		return status;
	}
}



my txt file is:
mynava
1234abc

I get the same result (below) regardless if the username and password are correct or not.


Login

Username: wrong
Password: kljadlskjf

Attempts exceeded.
Attempts exceeded.
Attempts exceeded.
You have 3 remaining.

Login sucessful!

Invalid username or password.
Login

Username: kaljsd
Password: jdjdj

Attempts exceeded.
Attempts exceeded.
Attempts exceeded.
You have 3 remaining.
Press any key to continue . . .
Last edited on
Why do main() and Login() need to open the file?
I am not 100% I think I read somewhere to open and close them for each function? That and/or I was getting errors that pretty much had me code it this way. I can't really remember. I've been on this code for days on end now and have tried so many variations of the functions so my head is just clouded, honestly. I am literally dreaming of code.

Do you think opening the file in the main and the other funtion is part of my problem?
Last edited on
minava, there is many things you have to change in order for your code to do what it should. But don't get discouraged by that.

Broken down into steps, this is what you should do:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>
#include <string>

// Function prototypes
void login();
bool checkcredentials(string, string);

int main()
{
   1.1] call the login function and let it do its work
    
   return 0;
}


// Login function
void login()
{
   1.] Define two string objects:
        username
        password
   3.] Define an int variable that acts as counter for your loop
   2.] Use a do/while loop 

   do  
   {
      1.] Let the count variable decrement;
      2.] Ask for a username
      3.] Ask for a password

           Call the boolean function that checks the credentials and pass to it 
the username and password.
      4.1] If (function(name, pass) == true)
             {
                 4.1.1 Output to screen that the user has been logged in successfully
             }
      4.2] Else
             {
                  If count is greater than 0
                  {
                     4.2.1] Output the number of tries left
                  }          
                  Else 
                  {
                     4.2.3] Inform the user that his or her credentials could not be verified.
                  }       
             }
        } while (count > 0 && checkCredentials(name, pass) != true);

        If count reaches 0, the function will return to main();
  }
}

// checkCredentials function
bool checkCredentials(string name, string pass)
{
       1.] Define two string objects:
            username
            password
            Define a boolean variable bool status = false;

      1.1] Instantiate a local fstream variable
      1.2] Open the file
      1.4] If the file has been opened successfully
      
      if (inFile)
      {
          1.5] Read in the data from the file
          while(!inFile.eof())
          {
             
             1.6] inFile >> username >> password;
             1.7] Now you have to write an if/else statement 
                  that compares the username and password 
                  read in from the file to the one passed to this function
             1.7.1] if (name = username && pass == password)
                       {
                           1.7.2] status = true;
                       }
                       else
                       {
                           1.7.3] status = false;
                       }
          }
      }
      
      1.9] close the file
      1.10] return status to caller;
}


It should not be hard to translate this into code. Give your best! :-)
Last edited on
Thank you so much Misenna! I made the changes as best I could and the program is running, however, I messed up somewhere along the way. The counter function is not showing remaining attempts and also I can't get it to verify the username and password from the txt file. see my code and outputs below:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void Login();
bool CheckCredentials(string, string);

int main()
{
	cout << "Login" << endl;
	Login();
	
	return 0;
}

void Login()
{
	string username;
	string password;

	int attempts = 0;

       for (int i = 0; i <attempts; i++)
	{
		attempts = - 1;
	}

	do
	{
		attempts = attempts - 1;

		cout << "\nUsername: ";
		cin >> username;
		cout << "Password: ";
		cin >> password;

		CheckCredentials(username, password);

		if (CheckCredentials(username, password) == true)
		{
			cout << "\nLogin sucessful!" << endl;
		}
		else
		{
			if (attempts > 0)
			{
				cout << "You have " << attempts << " remaining.\n";
			}
			else
			{
				cout << "\nInvalid username or password. " << endl;
			}
		}

	} while (attempts > 0 && CheckCredentials(username, password) != true);

	if (attempts == 0)
		return Login();
}


bool CheckCredentials(string user, string pass)
{
	string u;
	string p;

	bool status = false;

	ifstream inFile;
	inFile.open("CorrectData.txt");

	if (inFile)
	{
		while (!inFile.eof())
		{
			inFile >> user >> pass;
			if (user == u && pass == p)
			{
				status = true;
			}
			else
			{
				status = false;
			}
		}
	}
	inFile.close();
	return status;
}


outputs: 1st one doesn't stop looping after 3 attempts

Login

Username: mymymy
Password: nonono

Invalid username or password.
Username: myra
Password: lasdflhk

Invalid username or password.
Username: jajajkd
Password: klaksjdfl

Invalid username or password.
Username: mynava
Password: 1234abc

Invalid username or password.


this is the valid username and password but it's reading it as invalid and continues looping.
1
2
3
4
5
6
7
Login

Username: mynava
Password: 1234abc

Invalid username or password.
Username:
Well done! Now, there is really only some small mistakes, but nothing major. Here is the parts you should change and it should work. :-)

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
void Login()
{
	string username;
	string password;

        // Initialize this variable to a value of 3 (should have mentioned that, my bad!)
	int attempts = 3;
        
         // I removed the for loop, this is not necessary 

	do
	{
                // Change this to a simple attempts--;
	        attempts = attempts - 1;

		cout << "\nUsername: ";
		cin >> username;
		cout << "Password: ";
		cin >> password;
                
                // You do not need to call the function seperately, 
                // since you are already calling it in the if/else clause 
                // Delete it!
		// CheckCredentials(username, password);

		if (CheckCredentials(username, password) == true)
		{
			cout << "\nLogin sucessful!" << endl;
		}
		else
		{
			if (attempts > 0)
			{
				cout << "You have " << attempts << " remaining.\n";
			}
			else
			{
				cout << "\nInvalid username or password. " << endl;
			}
		}

	} while (attempts > 0 && CheckCredentials(username, password) != true);

        // Spotted this one too late - You can delete these lines
        // If a condition in the do/while loop is met the function
        // will return to caller - Remember it is a void function, 
        // it does not return anything, and would only produce
        // an error
	// if (attempts == 0)
	//	return Login();
}


On to the checkCredentials function

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
bool CheckCredentials(string user, string pass)
{
	string u;
	string p;

	bool status = false;

	ifstream inFile;
	inFile.open("CorrectData.txt");

	if (inFile)
	{
		while (!inFile.eof())
		{
                        // Here is a small mistake - look at the parameter names
                        // you are passing to this function user and pass, now look
                        // at the now commented line - notice something? (Hint:
                        // you are overwriting the values passed to this function,
                        // by the values read in from your text file!)
			// inFile >> user >> pass;

                        // What you want is this:
                        inFile >> u >> p;

			if (user == u && pass == p)
			{
				status = true;
			}
			else
			{
				status = false;
			}
		}
	}
	inFile.close();
	return status;
}



To avoid such mistakes as the one in the CheckCredentials() function, you would use:

bool CheckStatus(const string user, const string pass)
{
...
}

If you try to assign anything to these variables, the compiler won't let you, and you would immediately know (or rather should), where the culprit is to be found.
Last edited on
Misenna, thank you so much for your help!! I made the adjustments plus a couple of my own since I still needed to show the "attempts exceeded" portion...
Here is my final code. Again thank you so much! It works perfectly now!
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void Login();
bool CheckCredentials(string, string);

int main()
{
	cout << "Login" << endl;
	Login();
	
	return 0;
}

void Login()
{
	string username;
	string password;

	int attempts = 3;

	do
	{
		attempts = attempts--;

		cout << "\nUsername: ";
		cin >> username;
		cout << "Password: ";
		cin >> password;


		if (CheckCredentials(username, password) == true)
		{
			cout << "\nLogin sucessful!" << endl;
		}
		else
		{
			if (attempts > 0)
			{
				cout <<"\nInvalid username or password. " << endl;
				cout << "You have " << attempts << " remaining.\n";
			}
			else
			{
				cout << "\nAttempts exceeded." << endl;
			}
		}

	} while (attempts > 0 && CheckCredentials(username, password) != true);
}


bool CheckCredentials(string user, string pass)
{
	string u;
	string p;

	bool status = false;

	ifstream inFile;
	inFile.open("CorrectData.txt");

	if (inFile)
	{
		while (!inFile.eof())
		{
			inFile >> u >> p;
			if (user == u && pass == p)
			{
				status = true;
			}
			else
			{
				status = false;
			}
		}
	}
	inFile.close();
	return status;
}
Topic archived. No new replies allowed.