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

I NEED SOME HELP TO FIGURE THIS OUT. ANY SUGGESTIONS OR IDEAS PLEASE!! I need to

ID: 3624163 • Letter: I

Question

I NEED SOME HELP TO FIGURE THIS OUT. ANY SUGGESTIONS OR IDEAS PLEASE!!

I need to write a program the acts like a magic 8 ball providing answers to questions entered using the keyboard by the user. In actually the program is ignoring the question completely, and is simply choosing an answer stored in a text file.
I need to name the text file ANSWERS.TXT, containing several responses. When a question is input, the program will open the file, randomly select an answer, and print it to the screen and continues until the user enters the “ question” QUIT (in all caps, with no question mark at the end. Answers include:

That is a very good question.
I'm not sure, but I think you'll find the answer in your textbook.
You should ask your doctor that question.
If I were you, I wouldn't worry about such things.
That question has puzzled philosophers for centuries.
The answer to that question lies deep within the heart of all mankind.
I don't know, I'm just a machine.
Think about it, and the answer will come to you.
I used to know the answer to that question, but I've forgotten it.
The answer can be found in a secret place in the woods.
Reply hazy, try again later.

Programming considerations:

I need to use a global const integer called ANSWERS that records the number of responses in the file.The program will do its work by generating a random number (rand() % ANSWERS) to select the response, opening the file, and reading through the file until the desired answer has been read. The chosen response is then printed to the screen. (Note that this begins numbering the responses with 0.) Print an extra blank line after each response to make output more readable. Give the user the option when the program starts of logging output. If the user chooses to log output, ask the user for the file name, and store questions and their responses in the file. The file should be closed when the user is ready to quit.

Explanation / Answer

please rate - thanks

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int ANSWERS=0;
int main()
{char yes;
string buffer,quest;
bool log=false;
char filename[20];
int n,i;
ifstream in;
ofstream out;
srand(time(0));
in.open("ANSWERS.TXT");       
if(in.fail())           
   { cout<<"input file did not open please check it ";
   system("pause");
   return 1;
   }
getline(in,buffer);
while(in)
   {ANSWERS++;
    getline(in,buffer);
   
   }
in.close();
in.clear();
cout<<"do you want to log output (y ): ";
cin>>yes;
yes=toupper(yes);
if(yes=='Y')
    {cout<<"enter the log file name: ";
     cin>>filename;
     out.open(filename);        
     log=true;
     }
getchar();
for(; ;)
{
cout<<"Enter question (Q to quit): ";
getline(cin, quest);
if(toupper(quest[0])!='Q')
     {in.open("ANSWERS.TXT");
      if(in.fail())           
         { cout<<"input file did not open please check it ";
           system("pause");
           return 1;
          }
      n=(rand() % ANSWERS);
      for(i=0;i<n;i++)
         getline(in,buffer);
       cout<<buffer<<endl;
      if(log)
          out<<quest<<endl<<"   "<<buffer<<endl;
       in.close();
       in.clear();
       }
else
      {if(log)
          out.close();
           return 0;
           }
}
}