Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Outputting random question set?

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


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!
(Feb 7, 2016 03:20 PM)Aus10 Wrote:  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?

Thanks!

This is why you pass arguments to functions. Take in the file, pick a random number/line that you want to keep it. Pass it as an argument to a function to output it using cout. That's about it.
I sort of understand, and tried it. However when you say take in the file, where should I do this? It seems I'm trying to pass a function as an argument into another function. A function into a function, I've been researching and can't seem to find examples. :/
Basically you'll make two functions. Pseudocode follows. No need to pass a function to a function.

Reading Function
Get/open file
Read it in, stop at random line.
Save that line to a string/etc as you need.
Call Output_function(string you got of that line)
Close file
End function

Output_function
Take the string it got from argument
cout << string << endl;
// other stuff you have to output, options, etc.
That clears alot up. Thank you very much for your help!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by cryptobassboostFeb 7, 2019 05:28 PM 4 219 Feb 7, 2019 05:28 PM
Last Post: Caliban
[Python] by Mik3yJan 22, 2018 08:43 AM 9 349 Jan 22, 2018 08:43 AM
Last Post: Annointed
[.NET] by SmokeyModemJan 20, 2016 06:21 PM 2 72 Jan 20, 2016 06:21 PM
Last Post: Hawk HF