Help with Thoughtcrime Detector

closed account (2L3UpfjN)
You are to write a program that is a thoughtcrime detector. Your program will take in as input the names of two files: the fi rst file will contain a list of acceptable words and the second is a book (i.e. a list of words) suspected of thoughtcrime. The program then compares each word in the second file to the
acceptable words: if an unacceptable word is found, the program outputs
"Thoughtcrime!"; otherwise "The best books. . . are those that tell you what you know already."

I have written most of the code, but I am having trouble with the last function

I don't understand how I can write the code to see if the last function (posted here), checks to see if there is actually a thoughtcrime or otherwise. I dont see how i can filter the code to go through the acceptable file vector, to check if it is a thoughtcrime.
This function is the function where I am attempting to check for thoughtcrimes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool is_book_acceptable(ifstream& f, vector<string>& words)
{
	// write your code here
	string str2;
	int x = 0;
	int y = 0;
	while (f >> str2)
	{
		string_to_lower(str2);
	}
	for (x; str2[x] != words.at(y); x++)
	{
		for (y; y < words.size(); y++)
		{
			return false;
		}
	}
	return true;
}



This is the rest of the code:
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

// Preconditions: str is initialized
// Postconditions: all upper case letters in str are now in lower case
void string_to_lower(string& str);

// Preconditions: f is a valid and open input file stream
//                words is initialized
// Postconditions: the lower case version of all the words in f have been put into words
void read_acceptable_as_lower(ifstream& f, vector<string>& words);

// Preconditions: f is a valid and open input file stream
//                words is a vector of acceptable words in lower case
// Returns: true if all the words in f appear in words (case insensitive); false otherwise
bool is_book_acceptable(ifstream& f, vector<string>& words);

int main()
{

	string fname_acceptable;
	string fname_suspect;

	cout << "Enter the name of the thinkpol approved list: ";
	cin >> fname_acceptable;

	cout << "Enter the name of the citizen text: ";
	cin >> fname_suspect;

	//

	ifstream f_acceptable;
	ifstream f_suspect;

	f_acceptable.open(fname_acceptable.c_str());
	if (f_acceptable.fail())
	{
		cout << "Inform the thinkpol - acceptable list compromised!" << endl;
		return 1;
	}

	f_suspect.open(fname_suspect.c_str());
	if (f_suspect.fail())
	{
		cout << "User error - please provide a valid citizen text." << endl;
		return 1;
	}

	//

	vector<string> acceptable_words;

	read_acceptable_as_lower(f_acceptable, acceptable_words);
	if (is_book_acceptable(f_suspect, acceptable_words))
	{
		cout << "The best books... are those that tell you what you know already." << endl;
	}
	else
	{
		cout << "Thoughtcrime!" << endl;
	}
	f_acceptable.close();
	f_suspect.close();

	return 0;

}

void string_to_lower(string& str)
{
	// write your code here
	int i = 0;
	int x = str.length();
	for (str[i]; i < x; i++)
	{
		if ((str[i] >= 'A') && (str[i] <= 'Z'))
		{
			str[i] = str[i] + ('a' - 'A');
		}
	}
}

void read_acceptable_as_lower(ifstream& f, vector<string>& words)
{
	string str1;
	while (f >> str1)
	{
		string_to_lower(str1);
		words.push_back(str1);
	}
}

bool is_book_acceptable(ifstream& f, vector<string>& words)
{
	// write your code here
	string str2;
	int x = 0;
	int y = 0;
	while (f >> str2)
	{
		string_to_lower(str2);
	}
	for (x; str2[x] != words.at(y); x++)
	{
		for (y; y < words.size(); y++)
		{
			return false;
		}
	}
	return true;
}


Thank you in advance.
You make it to complicated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool is_book_acceptable(ifstream& f, vector<string>& words)
{
	// write your code here
	string str2;
	while (f >> str2)
	{
		string_to_lower(str2);
		bool is_found = false;
		for (size_t y = 0; y < words.size(); y++)
		{
			if(words[y] == str2)
			{
			  is_found = true;
			  break;
			}
		}
		if(!is_found)
			return false;
	}
	return true;
}
Topic archived. No new replies allowed.