Challenge 42

Description

The purpose of this challenge is to use functions with file access (reading and writing).

Requirements

  1. Write a function bool check_file(string present, string & past). This function will open a file called past.txt and search for the existence of present. If present is found in the file, the function will return true and set the value of past to the matching past-tense word. If present is not found in the file, set the value of past to blank and have the function return false. This function does not cin or cout. The past.txt file will have the following format as shown below. Each line will contain two words; the first word is the present-tense, the second word is the past-tense. You do not need to create this file manually. This file will be created progressively by the add_word() function below.
    bite bit
    fight fought
    ring rang
  2. Write a function void add_word(string present, string past). This function will append to a file called past.txt, a line of text composed of present, a space, and past. This function does not cin or cout. This function does not check if present already exists; it will always add a line of text as described above. For example, if the function is called as add_word(“eat”, “ate”), then a line of text “eat ate” gets added to past.txt.
  3. Write a function bool ends_with_vowel(string text). This function returns true if the last letter is a vowel (a, e, i, o, u) and false otherwise. Remember that text is a string parameter, not C-string. text.length() gives you the length of the string. Remember that text[some integer value] allows you to access individual characters.
  4. In main(), ask the user to enter a word (verb). Call check_file() passing in the user’s word to see if the word is in the file. If the word is in the file (the function returns true), display the past-tense returned in the second parameter of the function call.
  5. If the check_file() function returns false, call ends_with_vowel() passing in the user’s word. Display to the user a suggestion that the past-tense of the entered word is verb+ed (if it ends with a consonant), or verb+d (if it ends with a verb). See sample interaction.  Then, ask the user if the suggested past-tense is correct. If the user says no, ask the user to type in the correct past-tense. Then, call add_word() to save the correct past-tense entered by the user.

Sample main()

int main()
{
  string verb, past, answer;
  
  cout << "Enter a verb: ";
  cin >> verb;

  if (check_file(verb, past))
  {
    cout << "The past tense is " << past << endl;
  }
  else
  {
    if (ends_with_vowel(verb))
      past = verb + "d";
    else
      past = verb + "ed";

    cout << "I think the past tense is " << past << endl;
    cout << "Is this correct (yes/no)? ";
    cin >> answer;

    if (answer == "no")
    {
      cout << "What is the correct past tense? ";
      cin >> past;

      add_file(verb, past);
    }
    else
      cout << "Great!" << endl;

  }

  return 0;
}

Sample Interaction / Output

Enter a verb: fly
I think the past tense is flyed. 
Is this correct? no
What is the correct past tense? flew

[run it again]
Enter a verb: fly 
The past tense is flew

[run it again]
Enter a verb: bake 
I think the past tense is baked
Is this correct (yes/no)? yes
Great!

[run it again] 
Enter a verb: comb 
I think the past tense is combed. 
Is this correct (yes/no)? yes
Great!

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL00042

Print Requirements