I'm trying to get the computer to compare a username and password entered by the user to a username and password in a text file. If a match is found, then I am going to present the user with other options. I think there must be an easier way to do this than the way I did. I will post the text file and my cpp file. Please help me figure out a better way to check for the usernames and passwords. Once I verify their information, I would like to store their information, including their address, etc, into strings and then close the file, so the file doesn't stay open the whole time.
Thank you!
First the text file--
green rebecca 1001 sycamore, commerce, tx. 75428
1031
10453.56
meneir johanas 2833 robust, rowlett, tx. 77893
9873
100.45
underwood damon 283 park, greenville, sc. 99232
1113
1024.75
of-arc joan 777 lemon-street, madrid, france 21212
2323
999.54
mouse minnie 999 disney, orlando fl. 55577
9986
325.67
heath vicki 8219 westley, greenville, tx. 75404
4569
4500.00
bindlish arti 90 west beach, city, ca. 22231
9003
23.00
pitt brad 100 coolsville, city, mi. 45699
3000
130.37
khan gangis 333 uroguy, city, mongolia 34343-00000
2003
340000.99
howard debbi 4741 holiday, city, il. 44557
2347
2400.00
slausages jakey 99834 junkyard, city, tx. 55554
1032
1000.00
Here is my code:
// Aaron Yoquelet ATM program
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void balance(); //was going to implement as a function
void deposit(); // Was going to be used later as a function
void withdrawal(); // was going to be used later
char fnuserentry(); // used as a menu for the user
int main()
{
double balance1 = 0; //captures the balance
double depositamount = 0; // captures amount of deposit
double withdrawalamount = 0; //withdrawal amount
string uname, pwd; //strings used to compare
char userentry = '1'; //user selection
do
{
cout << endl << "User Name : ";
cin >> uname;
if(uname.length() == 0) // Makes sure that they enter a name and not nothing
{
cout << endl << "Please enter a name" << endl;
continue;
}
break;
} while(1);
do
{
cout << endl << "Password : ";
cin >> pwd;
if(pwd.length() == 0)
{
cout << endl << "Password can't be empty" << endl; // So password can't be empty
continue;
}
break;
} while(1);
ifstream myinfile;
myinfile.open("ATM_data.txt"); //directs which file to use
if(!myinfile) //If the data file isn't there it'll tell you to find it
{
cout << endl << "Please find the file ";
system("PAUSE");
}
char c;
string unameFromFile, pwdFromFile, fake; //fake used in clearing uname
bool flag = false;
int intVal;
float floatVal;
do
{
c = myinfile.get();
if(myinfile.eof()) // end of file check
break;
else if(c != ' ')
unameFromFile += c;
else
{
do
{
c = myinfile.get();
if(c == '\n')
break;
} while(1);
if(uname == unameFromFile) //condition to continue
{
myinfile >> pwdFromFile; //gets pass from file
if(pwd == pwdFromFile) //checks for a match
{
flag = true;
system("CLS");
cout << endl << "Welcome" <<endl;
userentry = fnuserentry();
switch( userentry )
{ //PLANNED TO FUNCTION CALL ALL THESE CASES
case '1': myinfile.ignore(100, '\n');
myinfile >> balance1;
cout << "Your balance is $ " << balance1;
break;
case '2': myinfile.ignore(100, '\n');
myinfile >> balance1;
cout << "Type in amount to deposit $ : ";
cin >> depositamount;
balance1 = balance1 + depositamount;
cout << "Your balance is $ " << balance1;
break;
case '3': myinfile.ignore(100, '\n');
myinfile >> balance1;
cout << "Type in amount to withdrawal $ : ";
cin >> withdrawalamount;
if (withdrawalamount > balance1) //Makes sure you don't overdraw
{
cout << "Can't do, will overdraw" <<endl;
}
else
{
balance1 = balance1 - withdrawalamount;
cout << "Your balance is now $ " << balance1;
}
break;
default: cout << "\nYou chose to exit\n";
}
}
else // fake stores an empty string. Didn't know for sure if this was right
{
myinfile >> floatVal;
c = myinfile.get();
c = myinfile.get();
unameFromFile = fake;
}
}
else
{
myinfile >> intVal;
myinfile >> floatVal;
c = myinfile.get();
c = myinfile.get();
unameFromFile = fake;
}
}
} while(1);
if(!flag)
cout << endl << "Username and Password don't match";
cout << endl << endl << "Quitting" <<endl;
system("PAUSE");
return(0);
}
char fnuserentry() //function to provide a menu
{
char userentry = 'a';
cout << "Press 1 to see your balance" <<endl;
cout << "Press 2 to make a deposit"<<endl;
cout << "Press 3 to make a withdrawal"<<endl;
cout << "Press 4 to quit"<<endl;
cin >> userentry;
return userentry;
}
<< moderator edit: added [code][/code] tags >>