Here is a general explanation of what each option should do: a. Read responses f
ID: 3586111 • Letter: H
Question
Here is a general explanation of what each option should do:
a. Read responses from a file:
This will read the responses and their category from the file responses.txt into an array.
b. Play Magic Eight ball:
User will ask a question, and a random response with a category is given from the array.
c. Print out responses and categories alphabetically:
Need to sort the responses in the array and print them.
d. Write responses to a file:
Write the array of responses with the categories (on separate lines) to a new file. You can name that file responsesOutput.txt.
Implementation Detail 1: Remember you will need to #include “functions.h” into both your main.cpp and functions.cpp.
Implementation Detail 2: You should have a “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.
Example:
struct Magic
{
string response;
string category;
}
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/
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
responses.txt
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
//File Name: functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include<string>
using namespace std;
//Structure Magic definition
struct Magic
{
//Member to store response
string response;
//Member to store category
string category;
};//End of structure
//Renames the Magic to M
typedef struct Magic M;
//To store number of records in the file
int numberOfRecords;
//To read the file contents and store it in structure member
void readFile(M[]);
//To display menu and return user choice
int menu();
//To generate and return random number
int randomNumber();
//To sort the response and display it
void printResponseCategory(M);
//To write data to file
void writeFile(M);
#endif
//File Name: functions.cpp
#include"functions.h"
#include<iostream>
#include <fstream>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
//To sort the response and display it
void printResponseCategory(M arr[])
{
//Loop variable
int x, y;
//To store the response for swapping
string Temp;
//Loops till number of records
for(x = 0; x < numberOfRecords; x++)
{
//Loops till number of records minus x and one times
for(y = 0; y < numberOfRecords - x - 1; y++)
{
//Checks if the current response is greater than the next response
if(arr[y].response > arr[y + 1].response)
{
//Swap response
Temp = arr[y].response;
arr[y].response = arr[y + 1].response;
arr[y + 1].response = Temp;
//Swap category
Temp = arr[y].category;
arr[y].category = arr[y + 1].category;
arr[y + 1].category = Temp;
}//End of if condition
}//End of inner for loop
}//End of outer for lop
//Loops till number of records
for(x = 0; x < numberOfRecords; x++)
{
//Display the data
cout<<setw(10)<<"Response: "<<setw(40)<<std::left<<arr[x].response<<setw(10)<<"Category: "<<setw(30)<<std::left<<arr[x].category<<endl;
}//End of for loop
}//End of function
//Function to generate and return random number
int randomNumber()
{
//To store random number
int num;
//Initialize random seed:
srand (time(NULL));
//Generates a random number between 1 and number of records
num = rand() % numberOfRecords + 1;
//Returns the random number
return num;
}//End of function
//Displays the menu, accept user choice and return it
int menu()
{
//To store user choice
char choice;
//Displays menu
cout<<" a) Read responses from a file";
cout<<" b) Play Magic Eight Ball";
cout<<" c) Print out responses and categories alphabetically";
cout<<" d) Write responses to a file";
cout<<" e) Exit";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>choice;
//Returns user choice
return choice;
}//End of function
//Function to read two numbers from responses.txt file
void readFile(M arr[])
{
//Initialize the record counter to zero
int c = 0;
//ifstream object created
ifstream inFile;
//Opens file for reading data from responses.txt file
inFile.open ("responses.txt");
//Loops till not end of file
while(!inFile.eof())
{
//Reads first line and stores it
getline(inFile, arr[c].response);
//Reads second line and stores it
getline(inFile, arr[c].category);
//Increase the counter by one
c++;
}//End of while
//close the file
inFile.close();
//Stores the counter value in number of records
numberOfRecords = c;
}//End of function
//Function to write data to file
void writeFile(M arr[])
{
//ofstream object created
ofstream outFile;
//Opens file for writing data to output.txt file
outFile.open ("output.txt");
//Loops till number of records
for(int c = 0; c < numberOfRecords; c++)
{
//Writes data to file
outFile<<arr[c].response<<endl;
outFile<<arr[c].category<<endl;
}//End of for loop
//close the file
outFile.close();
}//End of function
//File Name: main.cpp
#include"functions.cpp"
#include<iostream>
#include<stdlib.h>
#define MAX 50
using namespace std;
//Main function definition
int main()
{
//Creates an array of structure objects
M magicResponse[MAX];
//To store choice
char choice;
//To store random number
int number;
//To store question asked by the user
string question;
//Reads file
readFile(magicResponse);
//Loops till user choice is not 'e' or 'E'
do
{
//Displays the menu and stores user choice
choice = menu();
//Switch case begin
switch(choice)
{
case 'a':
case 'A':
//Calls the function to read data from file
readFile(magicResponse);
break;
case 'b':
case 'B':
//Calls the function generate random number and stores it
number = randomNumber();
//Accepts user question
cout<<" Ask a question: ";
cin>>question;
//Cleans standard input
fflush(stdin);
//Displays information
cout<<magicResponse[number].response<<" "<<magicResponse[number].category;
break;
case 'c':
case 'C':
//Calls the function to sort the response and display it
printResponseCategory(magicResponse);
break;
case 'd':
case 'D':
//Calls the function to write data to file
writeFile(magicResponse);
break;
case 'e':
case 'E':
exit(0);
default:
cout<<" Invalid choice!";
}//End of switch-case
}while(1);//End of do-while
}//En of main
Sample Run:
a) Read responses from a file
b) Play Magic Eight Ball
c) Print out responses and categories alphabetically
d) Write responses to a file
e) Exit
Enter your choice: a
a) Read responses from a file
b) Play Magic Eight Ball
c) Print out responses and categories alphabetically
d) Write responses to a file
e) Exit
Enter your choice: b
Ask a question: Can I pass the exam?
Don't count on it negative
a) Read responses from a file
b) Play Magic Eight Ball
c) Print out responses and categories alphabetically
d) Write responses to a file
e) Exit
Enter your choice: c
Response: As I see it, yes Category: positive
Response: Ask again later Category: vague
Response: Better not tell you now Category: vague
Response: Cannot predict now Category: vague
Response: Concentrate and ask again Category: vague
Response: Don't count on it Category: negative
Response: It is certain Category: positive
Response: It is decidedly so Category: positive
Response: Most likely Category: positive
Response: My reply is no Category: negative
Response: My sources say no Category: negative
Response: Outlook good Category: positive
Response: Outlook not so good Category: negative
Response: Reply hazy try again Category: vague
Response: Signs point to yes Category: positive
Response: Very doubtful Category: negative
Response: Without a doubt Category: positive
Response: Yes Category: positive
Response: Yes definitely Category: positive
Response: You may rely on it Category: positive
a) Read responses from a file
b) Play Magic Eight Ball
c) Print out responses and categories alphabetically
d) Write responses to a file
e) Exit
Enter your choice: d
a) Read responses from a file
b) Play Magic Eight Ball
c) Print out responses and categories alphabetically
d) Write responses to a file
e) Exit
Enter your choice: e
output.txt file contents
As I see it, yes
positive
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
It is certain
positive
It is decidedly so
positive
Most likely
positive
My reply is no
negative
My sources say no
negative
Outlook good
positive
Outlook not so good
negative
Reply hazy try again
vague
Signs point to yes
positive
Very doubtful
negative
Without a doubt
positive
Yes
positive
Yes definitely
positive
You may rely on it
positive
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.