Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Assignment: “Mystery Helper\" After a famous writer of mystery stories died,

ID: 3672833 • Letter: L

Question

Lab Assignment: “Mystery Helper" After a famous writer of mystery stories died, a cardboard wheel was found pinned to the wall of his study. This wheel was marked like wedges of a pie, each wedge containing notations such as "Get knocked out" or "Go to another place." Apparently the writer spun the wheel whenever he was stuck for a plot change. Amused by this, Michael Crichton (the author of Jurassic Park, among many other novels) created his own version of the "plot wheel" by programming a computer to randomly produce plot fragments which might serve as spurs to a writer's creativity. In this assignment, you will be creating your own version of Crichton's "Mystery Helper" program. The attached file, "mystery fragments.txt", contains a collection of short plot fragments, one on each line. Write a program which opens the file, reads each of these fragments and adds it to a vector, then prompts the user to enter M" (for "More Help) or "Q" (for "Quit"). Each time the player enters "M" select a plot fragment at random from the vector and print it to the console. Try choosing "M" repeatedly after you get your program working. and you may find that the computer will begin to weave its own (nonsensical) story! Finally, add a counter to your program so that, after every four times the player asks for help, your program prints the message "YOU WANT TOO MUCH HELP!!!" instead of one of the random fragments. Set the counter to zero at the start of the program, and check its value with each request for help; if the value equals four, print the message and reset it to zero again, and if it does not, increment it (that is, add one to its value) before printing a random fragment (you should define a constant instead of using the number 4" directly in your code). Your output should resemble the following: MYSTERY WRITER'S HELPER Enter 'H' to get help, or 'Q' to quit: h you smell smoke Enter 'H' to get help, or 'Q' to quit: h you find a hidden door Enter 'H' to get help, or Q to quit: h your flashlight suddenly goes out Enter 'H' to get help, or 'Q' to quit: h

Explanation / Answer

////////////////////////////// C++ code Myster_Helper.cpp ////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()

{
    cout << " MYSTERY WRITER'S HELPER ";
    cout << "======================== ";
    cout << " ";


    string line;
    vector<std::string> DataArray;                           // vecore storing the input from txt file
    ifstream myfile("mystery_fragments.txt");                // opening txt file given

    if(!myfile) //Always test the file open.
    {
        cout<<"Error opening output file"<< endl;
        return -1;
    }
    while (getline(myfile, line))
    {
        DataArray.push_back(line);                          // storing the data from file into vector
     
    }

    srand (time(NULL));
    char ch;
    int count = 0,num;
    while(1)
    {

      cout<< "Enter 'H' to get help, or 'Q' to quit: ";
      cin >> ch;
      if(ch == 'Q' || ch == 'q') break;
      count ++;
      if(ch== 'H' || ch == 'h' && count != 5)
      {
      num = rand() % DataArray.size();                      // generating random number
      cout<< DataArray[num].c_str();                        // out to the mystery
      cout <<" ";
      cout << " ";
      }
      if(count == 5 ) {count=0; cout<< "YOU WANT TOO MUCH HELP !!! "; cout << " "; }
   }

    return 0;
}