-1

Okay, so I was thinking of making a program that will help me in my studies. My idea was based off of flash cards and a random number generator. As I still have a LOT to learn in C++, I was wondering what some good approaches or examples would be for accomplishing my goal.

I'd like the program to output a random string from a predefined list to the user as well as go through all the defined strings before starting over again.

Simply put... What will I need to do in order to make a "generator" go through the predefined strings in a random order?

Colin S.
  • 3
  • 2
  • `std::map`, populate the map, then randomly choose the index. btw, this is not a good question for SO, as it is primarily opinion based. – vsoftco Apr 08 '15 at 22:48
  • Oh, my apologies. I was hoping it'd be okay as I'm not looking for a single way. I'll edit it. ^.^ – Colin S. Apr 08 '15 at 22:51
  • try reading this first: http://stackoverflow.com/help/how-to-ask – vsoftco Apr 08 '15 at 22:52
  • @vsoftco This isn't really opinion based. It might be too broad, but it's not opinion based: a program like that has a very logical flow. Also, what would `map` have that `vector` doesn't? Just curious. – Hailey Apr 08 '15 at 22:53
  • @Hailey when I said opinion based I meant that there will be lots of ways of doing it, depending on the preference. You just gave an example: `vector` vs `map`. And yes, it is also too broad. – vsoftco Apr 08 '15 at 22:54
  • "Primarily Opinion Based" does not mean "may have some variation in implementation". In fact, the description of POB specfically says "Many good questions generate some degree of opinion based on expert experience". – Hailey Apr 08 '15 at 22:58
  • 1
    Okay, please avoid arguing! Cheers! I tried to be more specific in my question. My apologies for that. – Colin S. Apr 08 '15 at 22:58

2 Answers2

1

Such a program would be composed of three parts:

  • Acquire the questions

The questions would have to be in a file with something like

What is the capital of Arkansas? Little Rock

Put them all in a text file, one per line. Read them in to an array of strings, like in this answer.

  • Print out random questions from the array and accept inputs.

Don't forget to split it into the question and answer based on the location of the ?. This means you'll need only one ? per question. If the answer meets the answer after the ?, increment your correct counter. Either way, increment your total counter.

  • compare the results

At the end, give the user a report of how many they got right or wrong.

Each of these are simple problems you can tackle one at a time, until you have your complete solution. :-)

When you're done, look at the

Community
  • 1
  • 1
Hailey
  • 318
  • 1
  • 6
0

If you want to go through all of the strings in random order, one way of doing it is generate a set of indexes from 0 to N-1 as a std::vector<size_t> idx, shuffle the vector idx using std::shuffle, then use the shuffled vector to loop through your vector of strings.

Here is a simple example of how this works:

#include <iostream>
#include <random>
#include <algorithm>
#include <numeric>
#include <utility>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());

    std::vector<std::pair<std::string, std::string>> qa
    {
        {"Capital of US", "Washington"},
        {"The baby in Family Guy", "Stewie"},
        {"Capital of Texas", "Austin"}
    };

    std::vector<std::size_t> idx(qa.size());
    std::iota(idx.begin(), idx.end(), 0u); // generate indexes
    std::shuffle(idx.begin(), idx.end(), gen); // shuffle the indexes
    // now display the elements in random order:
    for (std::size_t i = 0; i < qa.size(); ++i)
    {
        std::cout << qa[idx[i]].first << ": "
                  << qa[idx[i]].second << std::endl;
    }
}

std::shuffle guarantees a uniform distribution over all possible permutations.

vsoftco
  • 52,188
  • 7
  • 109
  • 221
  • Let's take this a step further... You run the program, it says "Capital of US", the user has to press enter to get "Washington". Click again to get the second question. Would this same method work? – Colin S. Apr 08 '15 at 23:24
  • @ColinS. in principle yes, but you have to add additional logic (`if/else`, reading the input etc.). If you're not familiar with that, you better get yourself a good introductory C++ book http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Basically Hailey's answer describes in a nutshell what you have to do, now try implementing it. What I wanted to mention in my answer was how to make sure you can go over all strings in random order, without repeating. – vsoftco Apr 08 '15 at 23:26
  • Wonderful. You both rock. - The basics are covered, the shuffles/indexes in the actual code threw me off a 'bit. Cheers! – Colin S. Apr 08 '15 at 23:29