Hello, I am fairly new to programming in C++ and I am having difficulties getting my code to work for my homework assignment. I code crashes whenever I attempt to run it and I am not sure why. I've included my code, the question, and the text file that my code will be reading from. The assignment is as follows:
Magic 8 Ball
-----------------------------------------------------------------------------
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as "I don't think SO", "Yes, of course!", "I'm not sure", and so forth. The program should read the responses from the file into an array or vector. It should prompt the user to ask a question, and then display one of the responses, randomly selected from the array or vector. The program should repeat until the user is ready to quit.
8_ball_responses.txt File:
-----------------------------------------------------------------------------
Yes, of course!
Without a doubt, yes.
You can count on it.
For sure!
Ask me later.
I'm not sure.
I can't tell you right now.
I'll tell you after my nap.
No way!
I don't think so.
Without a doubt, no.
The answer is clearly No.
Listed below is my code:
-----------------------------------------------------------------------------
int main()
{
const int Array_Size = 12;
string Magic_Eight_Ball[Array_Size];
bool temp;
int counter = 0;
ifstream inputFile;
// Open the file.
inputFile.open("8_ball_responses.txt");
// Read the contents from the file into the array.
while (counter < Array_Size)
{
getline(inputFile, Magic_Eight_Ball[counter]);
counter++;
}
// Close the file.
inputFile.close();
while(temp)
{
string question;
cout << "Please ask a question:\n";
cout << "(Enter 'QUIT' to exit)" << endl;
if (question.compare("QUIT") == 0)
{
temp = false;
}
else
{
int index = rand() % Array_Size;
cout << Magic_Eight_Ball[index];
}
}
return 0;
}
#include<iostream>
#include<fstream>
#include <string>
#include <cstdlib>
usingnamespace std;
int main()
{
constint Array_Size = 12;
string Magic_Eight_Ball[Array_Size];
bool temp;
int counter = 0;
ifstream inputFile;
// Open the file.
inputFile.open("8_ball_responses.txt");
// Check and see if the file does not open.
if(!inputFile)
{
cout << "Error finding input file" << endl;
system("pause");
exit(-1);
}
// Read the contents from the file into the array.
while (counter < Array_Size)
{
getline(inputFile, Magic_Eight_Ball[counter]);
counter++;
}
// Close the file.
inputFile.close();
while(temp)
{
// Create a string named question to hold user data
string question;
cout << "Please ask a question:\n";
cout << "(Enter 'QUIT' to exit)" << endl;
if (question.compare("QUIT") == 0)
{
temp = false;
}
else
{
// Call the random function
int index = rand() % Array_Size;
cout << Magic_Eight_Ball[index];
}
}
return 0;
}
8_ball_responses.txt File:
-----------------------------------------------------------------------------
Yes, of course!
Without a doubt, yes.
You can count on it.
For sure!
Ask me later.
I'm not sure.
I can't tell you right now.
I'll tell you after my nap.
No way!
I don't think so.
Without a doubt, no.
The answer is clearly No.
That's odd because temp was declared in line 12. I'm not sure why it is returning that error. But I've removed the system pause and exit from my code in lines 23 & 24 because they were giving me some errors.