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

Have you ever wanted to predict the future? Well the Magic Eight Ball does just

ID: 3597862 • Letter: H

Question

Have you ever wanted to predict the future? Well the Magic Eight Ball does just that. The original game was a softball sized “8-ball”. You would ask a question, shake it up and look at the result. There are 20 responses…10 positive, 5 negative, and 5 are vague. For this project, we want to recreate this, but give the ability to read in a set of responses, and add additional responses, and print out all of the responses in alphabetical order. Of course, we have to give seemingly accurate responses, which we will do by giving a random response.

Program Details:

You should have a menu with five lettered options. You should accept both capital and lower case letters in your menu options. The menu should do the task, then return to the menu (except in the case of exit). Any incorrect responses should get an error message, followed by a reprint of the menu.

Read responses from a file

Play Magic Eight Ball

Add responses to a file

Print out responses alphabetically

Print out responses by type (positive, negative, vague) (optional)

Write responses and questions to a file (optional)

Exit

Each menu item must be implemented using a function or sets of functions with appropriate input parameters and return values. Functions will have a prototype in a file called functions.h and defined in a file called functions.cpp. Also, you will have a struct that keeps track of the response and whether it is positive, negative, or vague. That struct will also be in the functions.h file.

Implementation Detail 1: Remember you will need to #include “functions.h” into both your main.cpp and functions.cpp but do not include .cpp files

Implementation Detail 2: You should have an “duplicate guard” in your functions.h like:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
//your code here
#endif

Implementation Detail 3: In most IDEs, you need to make sure all of your files are included in the project. For VS and csegrid, make sure all files are in the same directory. For XCode, you will need to place them in the same directory, then go to Product->Scheme->Edit Scheme and Use custom working directory where your .cpp and .txt files are located. For the csegrid run your program with
g++ -o pa2.o main.cpp functions.cpp. Then run with ./pa2.o

Implementation Detail 4: You must use (at least) one array, and struct. You may not use vectors or classes.

Implementation Detail 5: You can not have any global variables. So you will need to declare variables in main, then pass them (as appropriate) to your functions. Remember that there is no built in size for arrays, so you will have to pass the size (and MAXSIZE when trying to add to an array)

Implementation Detail 6: You will need to choose a random response. See the example at http://www.cplusplus.com/reference/cstdlib/rand/ (Links to an external site.)Links to an external site.

Note: You need some additional #include statements. You will use srand ONLY once, then use rand() with the modulus operator (%) to get a number between 0 and size -1

Extra Credit: If you have ALL of the functionality listed above (also optional cases), you can get up to 5 points extra credit.

Implementation Detail 6: Responses are as follows:

It is certain

positive

It is decidedly so

positive

Without a doubt

positive

Yes definitely

positive

You may rely on it

positive

As I see it, yes

positive

Most likely

positive

Outlook good

positive

Yes

positive

Signs point to yes

positive

Reply hazy try again

vague

Ask again later

vague

Better not tell you now

vague

Cannot predict now

vague

Concentrate and ask again

vague

Don't count on it

negative

My reply is no

negative

My sources say no

negative

Outlook not so good

negative

Very doubtful

negative

Explanation / Answer

Main.cpp

#include <iostream>

#include<string>

#include "functions.h"

using namespace std;

int main()

{

char ch='y';

int numlines;

string question;

while(ch!='F') {

cout << "--------------------------------" << endl;

cout << " Menu";

cout << " --------------------------------" << endl;

cout << " A. Read responses from a file" <<

" B. Play Magic Eight Ball" <<

" C. Add responses to the file" <<

" D. Print out responses" <<

" E. Print out responses by type" <<

" F. Exit (upper case)" <<

" Enter your choice: ";

cin >> ch;

switch (ch) {

case 'a': case 'A':

numlines = readResponses();

break;

case 'b': case 'B':

playGame();

break;

case 'c': case 'C':

addResponses(numlines);

break;

case 'd': case 'D':

printResponses(numlines);

break;

case 'e': case 'E':

printResponsesType(numlines);

break;

case 'f': case 'F':

break;

default:

cout << " Invalid choice";

break;

}

}

system("pause");

return 0;

}

functions.cpp

#include <iostream>

#include <fstream>

#include <stdlib.h>

#include <algorithm>

#include <cstring>

#include <string>

#include <ctime>

#define MAXSIZE 100

using namespace std;

struct response {

string resp[MAXSIZE];

string type[MAXSIZE];

int n;

}res;

int readResponses()

{

ifstream read;

string responses[MAXSIZE];

int numlines = 0;

string line;

int i = 0;

read.open("D:\Responses.txt");

while (getline(read, line))

{

++numlines;

res.resp[i] = line;

i++;

}

cout << "Responses are read" << endl << endl;

read.close();

return numlines;

}

void playGame()

{

string question;

srand(time(NULL));

string answer;

cin.ignore();

cout << "Enter your question: ";

getline(cin, question);

int x = rand() % (20 - 1) + 1;

answer = res.resp[x];

for (int i = 0;i < answer.length();i++)

cout << answer[i + 1];

cout << endl << endl;

}

void addResponses(int numlines)

{

string line;

int x = numlines + 1;

cin.ignore();

cout << "Write your response(starting with p or n or v (example: pTrue) without space): ";

getline(cin, line);

res.resp[x] = line;

numlines++;

ofstream write("D:\Responses.txt", ios::app);

if (write)

{

write << line << " ";

write.close();

}

cout << "your response is added" << endl << endl;

}

void printResponses(int numlines)

{

ifstream read;

string responses[MAXSIZE];

string line, str;

int i = 0;

read.open("D:\Responses.txt");

while (getline(read, line))

{

str = line;

for (int i = 0;i < str.length();i++)

cout << str[i + 1];

cout << endl;

}

cout << endl;

read.close();

}

void printResponsesType(int numlines)

{

string str, str1;

ifstream read;

read.open("D:\Responses.txt");

int i = 0, num = 0;

while (getline(read, str))

{

++num;

res.type[i] = str;

i++;

}

cout << " Positive responses are" << endl;

cout << "--------------------------------" << endl;

for (int j = 0;j < num;j++)

{

str1 = res.type[j];

if (str1[0] == 'p')

{

for (int i = 0;i < str1.length();i++)

cout << str1[i + 1];

cout << endl;

}

}

cout << " Negative responses are" << endl;

cout << "--------------------------------" << endl;

for (int j = 0;j < num;j++)

{

str1 = res.type[j];

if (str1[0] == 'n')

{

for (int i = 0;i < str1.length();i++)

cout << str1[i + 1];

cout << endl;

}

}

cout << " Vauge responses are" << endl;

cout << "--------------------------------" << endl;

for (int j = 0;j < num;j++)

{

str1 = res.type[j];

if (str1[0] == 'v')

{

for (int i = 0;i < str1.length();i++)

cout << str1[i + 1];

cout << endl;

}

}

cout << endl;

read.close();

}

functions.h

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int readResponses();
void playGame();
void addResponses(int);
void printResponses(int);
void printResponsesType(int);

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote