You are to write a program that is a thoughtcrime detector. Your program will take in as input the names of two files: the first 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++)
{
returnfalse;
}
}
returntrue;
}
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
usingnamespace 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++)
{
returnfalse;
}
}
returntrue;
}