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.********
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.
#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
}
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
returnfalse;
}
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
}
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';
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).
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)
{
returnfalse;
}
i++;
}
returntrue;
// write your code here
}