Question

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

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. 1. Read responses/categories from a file 2. Play Magic Eight Ball 3. Sort by responses 4. Sort by categories 5. Write responses/categories to a file 6. Delete response [EXTRA CREDIT and optional] – not for outline 7. Add response [EXTRA CREDI and optional] – not for outline 8. 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 placed above main(), as in the requirements and they will be defined in plain English and placed after main() { }, also in the requirement. • Below you have the function prototypes that you need in order to develop your plain English algorithm and attend the requirements. Functions Prototypes void readResponses(ifstream &infile, string responses[], string categories const int MAXSIZE, int &size ); void playMagicEightBall(string responses [], string categories [], int size ); void sortByResponses(string responses [ ], string categories[ ] , int size); void sortByCategories(string responses [], string categories [], int size ); void writeMagicResponseCategorie (ofstream &outfile, string responses [], string categories [], int size); void deleteMagicResponse(string responses[], string categories[], int &size); void addMagicResponse(string responses[], string categories[], int &size, const int MAXSIZE); • You cannot 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) o MAXSIZE should be a constant variable. Implementation detail for “Read responses from a file” : • You must use two arrays of the same size (responses [] and categories []). One for the response and one for the category. • Position zero of the response array will correspond to position zero of the category array (so when using both the response and category, you will have to use two addresses). You may not use vectors structs or classes. o A text file containing the responses and categories will be provided for you. Implementation detail for “Play magic ball : • Ask for the user to enter a question to be answered by the magic ball • Stay in a loop of asking and answer questions for the user until the user type the word “quit”, then you may come back to main() ( you may use do/while statement at this point) • You will need to choose a random response, which is, you have to generate a random number in a range of the size of the array. Implementation detail for “Sort by responses and Sort by categories” • If we haven't gone over sorting yet, but if you look up the bubble sort, you will be able to make that work as well, just by following the example in the book. • For the Sorting function, explain in comments, in plain English HOW you might go about sorting the array. Don't worry about actually implementing this in C++ codePrint out the responses followed by category if you sort by response. Print out category followed by response if you sort by category. Implementation detail for “Write responses to a file” • In this option you just need to declare an output file, read both arrays (response and category) and write their content into a file. Generate one output file in the same layout as the input file. Extra Credit: This extra credit is not for the outline is just for the final submission, i.e., for the second part of PA2 project that will be available for you after the due date of PA2-outline, but you may start a plain English algorithm now for these optional functionalities. In order to get extra credit, ALL the functionalities listed above MUST BE working properly. You can get up to 5 points extra credit in the next submission, i.e., when you start to work on you C++ code for the final submission (I mean, a 45/40 in the final submission, not for the outline submission) Implementation detail for “Delete answer” (extra credit) : • Ask the user for a specific index number to delete. Note that static arrays cannot actually delete, so we have to shift everything to the left by overwriting the old value. • Don't forget to decrease size by one. • size is passed by reference since we will change it. • Note that you have to delete the same specific index in both arrays, response and category, because if you have a response in index 1 (response[1]) the respective category of that response will be in the array category[1]. Implementation detail for “Add answer” (extra credit): • Add a Response and the related Category to the end of the respective arrays if there is room. o Hint: To check if there is room on the array you have to compare the size with the MAXSIZE o Let the user knows if the array is too full to add. o Ask the user to enter a new response and a new category to be included in both arrays. o Don't forget to add one to your size! • size is passed by reference since we will change it. • For add answer, you will ask for the user to enter a new answer and a new category to be included in both arrays (response[] and category[] ). • Don’t forget to ensure the size is passed by reference, so when you add or delete and change the size, you are changing the size in main. Layout of the file Field type comment response string Contain the response for the magic eight ball. category string Contain the category of the response. Example of the file content: In that file you can see the first line corresponds to a response and the second line corresponds the category of the previous response. After that, you can see another response followed to another category and so on. Hint: you can see a pattern inside the file, that is, after reading a category you always have a response, so in order to read this file you have to make a loop that read a response from the file and put it into the array called response[] after that you read the next line from the file and put it into another array called category[]. Example how the arrays should look like after reading the file: index responses[] categories[] [0] It is certain positive [1] It is decidedly so positive [2] Reply hazy try again vague [3] My reply is no negative Note: The table above is just an example, the content of the array will depend on which response/category is coming first inside the file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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 << "\tMenu";

cout << "\n--------------------------------" << endl;

cout << "\nA. Read responses from a file" <<

"\nB. Play Magic Eight Ball" <<

"\nC. Add responses to the file" <<

"\nD. Print out responses" <<

"\nE. Print out responses by type" <<

"\nF. Exit (upper case)" <<

"\n\nEnter 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 << "\nInvalid 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 << "\n";

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 << "\nPositive 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 << "\nNegative 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 << "\nVauge 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

SCREENSHOT OF A CODE

Source Code: Main.cpp: include <iostream> #include<string> include functions.h using namespace std; int main () { char ch=case D: case www printResponses (numlines) break E case e: case wwww printResponsesType (numlines); break f: case F:int readResponses () wwwwn ifstream read; string responses [MAXSIZE]; 0; int numlines stringline; 0; int i read opn D:\\Respvoid addesponses int numlines) { string line; int x numlines + 1; = www cin.ignore (); cout Write your response (starting wi} void printRespon pe (int numlines { string str, strl; ifstream read; read opnD:\\Responses.txt); 0, num 0 int i while (gecout << strl[i + 1]; cout < endl; w ww.onnns ww wwww endl \nVauge responses are cout < << endl; cout www. for (int j 0;j num

OUTPUT:

Menu A. Read responses from a file B. Play Magic Eight Ball c. Add responses to the file D. Print out responses E Print out rMenu A. Read responses from a file B. Play Magic Eight Bali c. Add responses to the file D. Print out responses E. Print outMenu A. Read responses from a file B. Play Magic Eight Balï c. Add responses to the file D. Print out responses E. Print out

Add a comment
Know the answer?
Add Answer to:
Have you ever wanted to predict the future? Well the Magic Eight Ball does just that....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ write a simple menu program that displays a menu for the user in an...

    In C++ write a simple menu program that displays a menu for the user in an object-oriented technique. The menu should keep showing up until the user chooses to quit. Also, there is a sub-menu inside a menu. The user should get at least a line displayed after he or she chooses that particular option meaning if the user presses 1 for the read actors from the file. The output can look like "reading actors from a file" and then...

  • I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline...

    I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline (10 points). Create an outline in comments/psuedocode for the programming assignment below. Place your comments in the appropriate files: main.cpp, functions.h, dealer.cpp, dealer.h, dealer.cpp (as noted below). Place into a file folder named LastnamePA3, the zip the content and hand in a zip file to Canvas. Part II: PA3: Car Dealership (40 points) For Programming Assignment 3 you will be creating a program to...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • C++ There is a premade heap.h file. You will write a program that creates a heap,...

    C++ There is a premade heap.h file. You will write a program that creates a heap, and then asks the user to enter values which you will put in the heap (at least 8), and then display the heap Then your program will display a menu with the following choices, and perform the appropriate actions Display the heap Add an item to the heap Remove the largest item Exit The program will continue until the user chooses to exit, at...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data...

    Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data from arrays, linear search, and File I/O. In addition, you will learn to work with parallel arrays This program will assist an English-speaking user to build their vocabulary in Spanish This program will read a file containing a list of words in English and another containing the translation of those words in Spanish. The words and corresponding translations should to be stored in two...

  • NOTE – You may use C++ - but you MAY NOT use #include <string> The goal...

    NOTE – You may use C++ - but you MAY NOT use #include <string> The goal is to use C-Style character array based strings. You will put all of your code in this file. You may use any technique we have learned so far. Specifications: This assignment is split into several parts. The goal is to get you used to working with C/C++ run-time arrays and strings. Part 0 – Create a Menu You should create a menu that gives...

  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT