How do I <<"output sections of txt code in random order " << endl ;

I have posted this in the beginners forum, but I think what I want to do
may require a more skillful C++ programmer to guide me in the right direction.
I have a feeling it has something to do with maths, where to start is the problem.

Also I am new to this site so I don't know how to display the code like others.

I will keep this simple as I have the code written for my testing software program, I just want to add a feature of randomly generating the answer choice output order, I do not want to randomly output the full questions, but instead the choices (answers to the question), for example =


#include <string>
#include <iostream>
#include <limits>




cout <<" " << endl ; \\\\\\ space

cout <<"You are preparing to install a new application on " ; \\\\ question
cout <<"a customer's Windows XP computer. If there is a problem " ;
cout <<"with the installation you need to be able to quickly " ;
cout <<"revert the computer to the way it was before the installation." << endl ;

cout <<" " << endl; \\\\\\ space

cout<<"1. Use the (system restore utility) to set a " ; \\\\\\\\ answer choice 1
cout<<"restore point before running the application " ;
cout<<"setup program. " << endl ;

cout <<" " << endl; \\\\\ space

cout <<"2. Use your internet security and perform" ; \\\\\\\\\\ answer choice 2
cout <<" a scan of all disk drives " << endl;

cout <<" " << endl; \\\\\\ space

cout <<"3. Reinstall windows if the app fails " << endl ; \\\\\\ answer choice 3

cout <<" " << endl ; \\\\\\ space


As you can see I would like to output the answers, choices randomly as in answer choice 3 will appear at the top instead of 1. and so on, it doesn't matter if the numbers move with the txt as the numbers beside the
questions are txt.

If anyone can offer some help on this I would be grateful, basically to output the choices randomly, even if it means copy and pasting the code into each section thanks.

Also I have provided the relevant part of the code for the question, the input
will not make a difference for this example so I excluded it.


It's a 32bit console application.
Last edited on
Put the choices into an array (or a vector), and shuffle the sequence before printing out.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>

int main()
{
    std::srand( std::time(0) ) ; // once, right at the start

    const int NCHOICES = 5 ;
    const char* choices[NCHOICES] =
    {
        "Use the (system restore utility) to set a\n"
        "   restore point before running the application setup program.\n\n",

        "Use your internet security and perform a scan of all disk drives\n\n",

        "Reinstall windows if the app fails\n\n",

        "Pray for divine intervention\n\n",
        
        "Give up\n\n"
    };
    
    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::random_shuffle( choices, choices + NCHOICES ) ;
    for( int i = 0 ; i < NCHOICES ; ++i ) std::cout << i+1 << ". " << choices[i] ;
}

http://coliru.stacked-crooked.com/a/90c8bd04bc05755b
Thanks alot for the response, I will try this and let you know how I got on.

This code is new to me, looks like it might work :)

Last edited on
It works for the first question :) but after that when I try to reuse the code for the second question, it basically tells me that I have used it already (not so much in these words)

redeclaration of 'const int NCHOICES'
const int NCHOICES' previously declared here
redeclaration of 'const char* choices [3]'
const char* choices [3]' previously declared here


So close.... do you have any idea what to do, will I just change the names of the const, and char ?

Thanks for trying though.
Last edited on
Ok I have sorted it out by changing the names of the const, and char, it fully works with two questions, and I cannot see why it won't work with over 100 more once I change the names.


Thank you very much, you definitely know what your talking about, I have the final piece to my program thanks once again :)
Last edited on
Topic archived. No new replies allowed.