I'm writing a program to read from a blacklist and a whitelist of names and determine if the person is allowed to have access. However, when it reads from the files in the loops it seems to only pick up some of the names to check. Therefore, most of the time it skips to the end of the program. Am I using the loops wrong or reading the file wrong(the file is a plain txt file with about 10 names in each).
//This program asks user's name and checks if there on
// the white list or black list or neither
// then either denies or allows access
//ask users for name
cout << "Enter your first name: ";
cin>>name;
cout << endl;
//open whitelist
infilew.open("white-list.txt");
//check to see if name is on the whitelist
while (infilew >> test)
{
infilew>>innamew;
if (innamew == name)
{
cout << "Access Granted!";
cout << endl;
infilew.close();
return 0;
}
else {
}
}
//close the file
infilew.close();
//open blacklist
infileb.open("black-list.txt");
//check to see if the name is on the blacklist
while(infileb >> test)
{
infileb>>innameb;
//if name not on either list ask the user a ?
cout << "Promise you will behave? Answer Y/y: ";
cin >> answer;
cout << endl;
//if answer is yes, add user to whitelist
if (answer == "Y" || answer == "y")
{
outfilew.open("white-list.txt",ios::app);
outfilew <<endl<<name;
outfilew.close();
cout << "You are in. Keep the trust!";
cout<< endl;
}
//else add user to blacklist
else
{
outfileb.open("black-list.txt",ios::app);
outfileb <<endl<< name;
outfileb.close();
cout << "You are out and stay out!";
cout << endl;
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
//create variables
string answer;
string test; //not needed
string name;
string innamew;
string innameb;
ifstream infilew;
ifstream infileb;
ofstream outfilew;
ofstream outfileb;
//ask users for name
cout << "Enter your first name: ";
cin>>name;
cout << endl;
//open whitelist
infilew.open("white-list.txt");
//check to see if name is on the whitelist
while (infilew >> innamew) // this was your problem
{
infilew>>innamew;
if (innamew == name)
{
cout << "Access Granted!";
cout << endl;
infilew.close();
return 0;
}
else {} // not needed
}
//close the file
infilew.close();
//open blacklist
infileb.open("black-list.txt");
//check to see if the name is on the blacklist
while (infileb >> innameb) // this was your problem
{
infileb>>innameb;
if (innameb==name)
{
cout << "Access Denied!";
cout << endl;
infileb.close();
return 0;
}
else {} // not needed
}
//close the file
infileb.close();
//if name not on either list ask the user a ?
cout << "Promise you will behave? Answer Y/y: ";
cin >> answer;
cout << endl;
//if answer is yes, add user to whitelist
if (answer == "Y" || answer == "y")
{
outfilew.open("white-list.txt",ios::app);
outfilew << endl << name;
outfilew.close();
cout << "You are in. Keep the trust!";
cout<< endl;
}
//else add user to blacklist
else
{
outfileb.open("black-list.txt",ios::app);
outfileb << endl << name;
outfileb.close();
cout << "You are out and stay out!";
cout << endl;
}
return 0;
}