Creating a String Array that reads string from a file

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:
-----------------------------------------------------------------------------

#include<iostream>
#include<fstream>
#include <string>
#include <cstdlib>

using namespace std;

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;
}
Use code tags:



[code]

//Your code here

[/code]



It'll make your code readable.

If your code is crashing, perhaps make sure that the file you're trying to open actually does open!:


1
2
3
4
5
if(!inputFile.isopen())
{
     std::cout << "The file didn't open!";
     return 0;
}



Add this code after you open the file to check if it actually opened.
I've update my code to check and see if it is opening the file but it is not displaying the Error Statement when I run my code.


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

#include<iostream>
#include<fstream>
#include <string>
#include <cstdlib>

using namespace std;

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");

    // 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;
}
Should probably show the contents of the txt file. Use a debugger to see where in the code specifically it crashes, that'll be useful.
The contents of the text are as follows:

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.
I've tried testing my code to display the elements of the array but it does not print anything and crashes.
In VS 2017 I got the following error:
Error C4700 uninitialized local variable 'temp' used on line 36

The main problem is that you don't read an answer after line 42 so question will always be empty
Last edited on
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.
Thank You! That fixed the issue I was having.
Topic archived. No new replies allowed.