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...
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
constint 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
}