Reading from files

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

//10/03/2012
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
//create variables
string answer;
string test;
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 >> 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 (innameb==name)
{
cout << "Access Denied!";
cout << endl;
infileb.close();
return 0;
}

else {
}
}

//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;
}
You just needed to change the conditions of your while loops

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
91
92
93
94
#include <iostream>
#include <string>
#include <fstream>
using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.