So here is the backstory in order to understand this issue!
I am using one function to read in questions and answer choices from a .txt file.
However, instead of just using the read in function to cout the text in the file...
I want to use a different function to cout a random question from the file.
How would I go about doing this?
My goal here is to randomly read in a question and its set of answer choices, one at a time, while including the A. B. C. D. in front of each answer choice. ( ABCD is not in the .txt file ).
Here is my code thus far
...
Thanks!
I am using one function to read in questions and answer choices from a .txt file.
However, instead of just using the read in function to cout the text in the file...
I want to use a different function to cout a random question from the file.
How would I go about doing this?
My goal here is to randomly read in a question and its set of answer choices, one at a time, while including the A. B. C. D. in front of each answer choice. ( ABCD is not in the .txt file ).
Here is my code thus far
...
PHP Code
<?php
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
// Function Prototypes
double read_questions();
double read_answers();
void show_question();
int main()
{
read_questions();
read_answers();
show_question();
return 0;
}
double read_questions()
{
string question;
string choice1;
string choice2;
string choice3;
string choice4;
string space;
fstream fin;
string input_arr[50][50];
fin.open("questions.txt");
for (int q = 0; q < 7; q++)
{
getline(fin, question);
getline(fin, choice1);
getline(fin, choice2);
getline(fin, choice3);
getline(fin, choice4);
getline(fin, space);
input_arr[q][0] = question;
input_arr[q][1] = "A. " + choice1;
input_arr[q][2] = "B. " + choice2;
input_arr[q][3] = "C. " + choice3;
input_arr[q][4] = "D. " + choice4;
input_arr[q][5] = space;
//cout << input_arr[q][0] << endl;
//cout << input_arr[q][1] << endl;
//cout << input_arr[q][2] << endl;
//cout << input_arr[q][3] << endl;
//cout << input_arr[q][4] << endl;
//cout << input_arr[q][5] << endl;
// I have these couts commented out b/c while this works perfectly,
// this function is only supposed to read in the file, not output it.
// My main issue is here, if I could somehow figure out how to
// get this output, but in the show_question() function, while
// still being completely random as to which question it chooses to output.
}
}
//reads in the answers from file, storing to a 1D array of characters
double read_answers()
{
fstream fin;
char answer_arr[50];
char answer;
fin.open("answers.txt");
for (int y = 0; y < 7; y++)
{
fin >> answer;
answer_arr[y] = answer;
}
}
//Displays a question and its responses, labeled A-D, optionally hiding a //specified response for the second try at a question.
//Its primary parameter is an array of strings that is one single //question/response set.
void show_question()
{
int random_number;
// seeds rand (needed for getting random numbers)
srand(time(NULL));
// randomly generates a number 0 - 7 inclusively
random_number = rand() % 7 + 0;
cout << "You roll a " << random_number << ": ";
}
Thanks!

