Text File and Input Data

Hi,

I'm having the upmost hardest time doing this program that basically validates the user's input with a text file. The text file is like so and below that is the work I have so far...

"user1"
"pass1"
"user2"
"pass2"
etc!

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
 string username, password, userinput, userpass;

   ifstream data("logins.txt");
       
  for (int i=0; i<10; i++)
  {
   getline (data, username[i]);
   getline (data, password[i]);
  }

 myfile.close();
 
	cout << "Username: " << endl;
	getline(cin, userinput);

 for (int i=0;i<10; i++)
 {
 if (userinput == username[i])
	{
	Found = true;
	
	cout << "Password: " << endl;
	getline(cin, userpass);

	if (userpass == password[i])
	{
		cout << "Access Granted" << endl;
	}
	else 
	{
		cout << "Username and Password do not match!"<< endl;
	}
	}
 }

			if (Found == false)
			{
				cout << "Username Not Found" << endl;
			}

	system("PAUSE");
	 return 0;
}
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    const int NUSERS = 10 ; // avoid magic numbers

    // string username, password, userinput, userpass;
    // usenames and passwords are read into an array
    string username[NUSERS], password[NUSERS] ;

    {
        ifstream data("logins.txt");

        // document assumptions
        // invariant: the file contains at least 10 pairs
        // of user names and passwords, one entry per line

        // for (int i=0; i<10; i++)
        for( int i = 0 ; i < NUSERS ; ++i )
        {
            getline (data, username[i]);
            getline (data, password[i]);
        }
        // when the stream goes out of scope, the destructor will close it.
    }

    //myfile.close();

    cout << "Username: " << '\n' ; // endl;
    string userinput ;
    getline(cin, userinput);

    bool Found = false ;

    //for (int i=0; i<10; i++)
    for( int i = 0 ; i < NUSERS ; ++i )
    {
        if (userinput == username[i])
        {
            Found = true;

            cout << "Password: " << '\n' ; // endl;
            string userpass ;
            getline(cin, userpass);

            if (userpass == password[i])
            {
                cout << "Access Granted" << '\n' ; // endl;
            }
            else
            {
                cout << "Username and Password do not match!"<< '\n' ; // endl;
            }
        }
    }

    if (Found == false)
    {
        cout << "Username Not Found" << '\n' ; // endl;
    }

    //system("PAUSE");
    // return 0; // there is an imlicit return 0 at the end of main
}
@jlborges

Thank you so much for your help!
Last edited on
Topic archived. No new replies allowed.