Description
The purpose of this challenge is to read and write files. This challenge will very loosely simulate a word game (think Words With Friends or Scrabble).
Requirements
- Acquire the file called enable1.txt. This file contains all the words that are considered valid in Words With Friends
- Create a function bool is_word_valid(string word). This function will open and read the enable1.txt file one line at a time searching for the existence of word. If the entire file is read and the word is not found, return false. If the word is found in the file, return true.
- Create a function int get_uses(string word). This function will try to open a file named word.uses where word is the string parameter. For example if the function is called as get_uses(“apple”), then the function will look for a file called apple.uses. The file apple.uses will contain a single integer value which represents the number of times apple has been used. If the file doesn’t exist, the function should return zero. If the file does exist, then return the integer value found in the file.
- Create a function void save_uses(string word, int count). This function will save to the word.uses file the value of count. Remember that the name of the file is based on the string parameter.
- Your program will ask the user to enter a word. Use a string variable to store the word entered by the user.
- Implement a loop in your main program to repeatedly ask the user to enter a word. When the user enters a plus symbol, the loop should break and the program should end.
- In main, call the is_word_valid() function passing in the word entered by the user. If the word is valid, report on how many times it has been used by calling the get_uses() function. Additionally, if the word is valid, make sure to update the count by calling the save_uses() function.
Sample main()
int main() { string word; int uses; // implement a loop here cout << "Play your word: "; cin >> word; if (is_word_valid(word)) { uses = get_uses(word); uses = uses + 1; cout << "You've played " << word << " " << uses << " times\n"; // now save the number of uses save_uses(word, uses); } else cout << "That is not a valid word" << endl; }
Sample Interaction
[Run your program] Play your word: abcde That is not a valid word Play your word: apple You've played apple 1 time Play your word: juicy You've played juicy 1 time Play your word: apple You've played apple 2 times Play your word: + Thank you for playing [Run your program] Play your word: apple You've played apple 3 times Play your word: + Thank you for playing
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0028
Print Requirements