1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <iostream>
#include<cstdlib>
#include<string>
#include<fstream>
#include<ctime>
using namespace std;
void readFromfile(string filename, string response[], string category[], int& maxResponses);
void magicEight( string response[], string category[], int& maxResponses);
void sortBall (string filename, string response[], string category[], int& maxResponses);
int main (){
bool done=false;
int menu;
const int MAXSIZE = 100;
string response[MAXSIZE], category[MAXSIZE], answer, categories;
int size=0;
while(!done){
cout<<"Select one of the options below\n";
cout<<"1. Read responses from file\n";
cout<<"2. Play Magic 8 Ball.\n";
cout<<"3. Print out responses and categories alphabetically\n";
cout<<"4. Exit\n";
cin>>menu;
switch(menu){
case 1 :
cout<<"Read from file\n";
readFromfile("answers.txt", response, category, size );
break;
case 2:
cout<<"Play Magic 8 Ball\n";
magicEight( response, category, size);
break;
/*case 3:
cout<<"Print out responses alphabetically\n";
sortBall ( "answers.txt", response, category, size);
break;*/
case 4:
cout<<"You will now exit menu.\n";
done=true;
break;
default:
cout<<"Invalid option.\n";
}
}
return 0;
}
void readFromfile(string filename, string response[], string category[], int& maxResponses) {
string answer, cats;
int i = 0, j = 0, k = 0;
ifstream infile(filename);
while (getline(infile, answer)) {
if (answer.back() == '\r') {
answer.pop_back();
}
getline(infile, cats);
response[maxResponses]=answer;
category[maxResponses]=cats;
cout<<response[maxResponses]<<" "<<category[maxResponses]<<endl;
maxResponses++;
}
}
void magicEight( string response[], string category[], int& maxResponses) {
string question;
char reply;
srand(time(0));
int i = 0, j = 0, k = 0;
//do{
cout << "Hi! I am magic Eight ball, ask me a question.." << endl;
cin>>question;
int index= rand()%(maxResponses-1);
cout<<response[index]<<endl;
file answers.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
|