detector

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 need to use these:
void string_to_lower(string& str);
void read_acceptable_as_lower(ifstream& f, vector<string>& words);
bool is_book_acceptable(ifstream& f, vector<string>& words);


string_to_lower function takes as an argument a single string (passed by reference). This function
must convert each letter in the string to lower case.

read_acceptable_as_lower function takes as arguments a reference to a file with acceptable words,
as well as a reference to a vector in which to store all of the resulting words, once converted to lower case.


is_book_acceptable function takes as arguments the a reference to the suspect file and a
reference to the vector of acceptable words in lower case. You can assume the file is already properly
opened, and you should not close it within this function. It returns a boolean variable: true if the
suspect book only uses acceptable words, false otherwise. This function must read each word from the
file, convert it to lower case, find if it is in the acceptable word list, and return judgement accordingly.

******Just need a a little help to start, not asking for anyone to do it, as that is not fair and shouldnt be done.********



Thanks!

Last edited on
Can you post some code that you have tried?
Have you written some code to read text in from the file yet?
Have you written some code to break the text into a list of words yet?

Strikes me that when you have these, the rest of your task will become apparent.
Do you see how you could use these functions to solve the problem? Start by writing the functions and then write your main program using the functions.
The files are written already. Just need to write the code...

I dont know how to use these functions to solve...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{

}

void string_to_lower(string& str);



void read_acceptable_as_lower(ifstream& f, vector<string>& words);



bool is_book_acceptable(ifstream& f, vector<string>& words);
Here, I forgot to include this......


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
#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
}

void read_acceptable_as_lower(ifstream& f, vector<string>& words)
{
	// write your code here
}

bool is_book_acceptable(ifstream& f, vector<string>& words)
{
	// write your code here
	return false;
}
Oh I misunderstood. I thought you had to write the whole thing. My bad since you clearly stated that you need to write those three functions.

Try writing string_to_lower(). In pseudocode, you want to do this:
1
2
3
for each character in str
   if the character is upper case
      replace it with its lower case equivalent.

If you #include <cctype> then you'll have access to these handy functions:
1
2
int isupper(int ch)
int tolower(int ch)
See details here: http://www.cplusplus.com/reference/cctype/
I filled in this part, it is suppost to convert the letters in the string to lowercase. Is this correct??
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
void string_to_lower(string& str)
{
	char c;
	while (f_acceptable >> c)
	{
		if (c >= 'a' &&	c <= 'z')
			cout << c << endl;

		if (c >= 'A' &&	c <= 'Z')
			c -= ('A' - 'a');
	}
	// write your code here
}
Also, I don't know what to put where f_acceptable is because this was defined in main, so it is saying its undefined here.
I think you're missing the point of what string_to_lower() does. It takes a parameter str and changes it so all upper-case letters become lower case. That's it. It doesn't read from f_acceptable. It doesn't write to cout. It just changes the str parameter. In other words:
1
2
3
string x = "I just LOVE chocolate!";
string_to_lower(x);
cout << x << '\n';

will output
i just love chocolate!
okay, so will this is what you mean?
Thanks

1
2
3
4
5
6
void string_to_lower(string& str)
{
	string_to_lower(str);
	cout << str << '\n';
	// write your code here
}
This is what I have now. I need some help with the read_acceptable_as_lower and is_book_acceptable.... Thanks

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
void string_to_lower(string& str)
{
	int leng = str.length();
	for (int i = 0; i< leng; i++)
	{
		if (str[i] >= 'A'	&&	str[i] <= 'Z')
			str[i] += ('a' - 'A');
	}

	// write your code here
}

void read_acceptable_as_lower(ifstream& f, vector<string>& words)
{
	string letters;
	while (f >> letters)
	{
		string_to_lower(letters);
		words = words + letters;
	}

	// write your code here
}

bool is_book_acceptable(ifstream& f, vector<string>& words)
{
	string letters;
	while (f >> letters)
	{
		string_to_lower(letters);
		if (read_acceptable_as_lower(letters))
		{
			return true;
		}
	}
	return false;
	// write your code here
}
A few comments:
Why not use tolower() as dhayden suggested instead of writing your own code. One less thing to worry about testing.

Line 19: + operator is not defined for vectors. You want to use words.push_back(letters);

Line 31: read_acceptable_as_lower does not test if the word read is in the vector.

Lines 13,25: You're passing the same ifstream to both functions. The instructions say there are two different file. A file containing a book and a file containing a list of acceptable words.

Lines 31-33: Your logic is flawed here. If any word is not acceptable, you want to reject the book (return false). Only when you've tested that all words are acceptable do you want to return true (line 36).
Last edited on
in the directions it said we cannot use tolower and the libraries. Thanks
Okay, so I made some of your corrections. I have an issue, no matter if I enter the suspect good or suspect bad file, I always get thoughtcrime.....

Thank you!

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
void string_to_lower(string& str)
{
	int leng = str.length();
	for (int i = 0; i< leng; i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
			str[i] += ('a' - 'A');
	}

	// write your code here
}

void read_acceptable_as_lower(ifstream& f, vector<string>& words)
{
	string letters;
	while (f >> letters)
	{
		string_to_lower(letters);
		words.push_back(letters);		// puts a new element at the end of the vector, increases container size by one
	}
}

bool is_book_acceptable(ifstream& f, vector<string>& words)
{
	string letters;
	int i = 0;
	while (f >> letters)
	{
		string_to_lower(letters);
		
		if (words[i] == letters)
		{
			return false;
		}
		i++;
	}
	return true;
	// write your code here
}
Topic archived. No new replies allowed.