Hi everybody I need some professional help from experienced people. I am writing a program in C++ about a game. I have to specify a class Questions with attributes question, answers and difficulty. In the program i should have methods add, edit, select, search, print and remove. The problem that i have is that i have to work dynamically with these questions and when i choose from the menu save, just then to save everything into the file. So far i have ideas for everything except what kind of container to use?? I thought that i can use "map" but the key ( this should be the number of question) is const and if i delete a question the keys after that will remain the same. I need when i delete a question this integer ( question number) to decrease with 1. Can you give me an advise what kind of container to use and if i need to put question number as an attribute into the class.
Thank you in advance for the help

Recommended Answers

All 4 Replies

A vector would be an option.

I think that you should make a distinction between the question's ID and its number in the test. I would make the ID constant throughout the life of the question, but allow the number to change. You could do this by having a GenerateQuiz (or similar) function that goes through all the questions and a assembles them into a quiz. It's not until this function is called that a question needs to be alloacted its number. So, you could just store all the questions in a std::vector and then use the GenerateQuiz function to assemble the questions and add the numbers.

This method would allow the further advantage that you could assemble different quizes from the same question set. For example, your Question class might have a level attribute. You could then let the GenerateQuiz method take a parameter (an enum or something) that would let you assemble different types of quiz:

Quiz easyQuiz = GenerateQuiz( questions, grade1 );
Quiz hardQuiz = GenerateQuiz( questions, grade3 );

where, grade1 and grade3 are enum values that tells the method that only questions of that level should be in the test.

Have fun :)

I have a restriction to use a rand() function to generate the question set. This will be easy because i will just take the Questions with same difficulty level. But is vectro a nice container . As i read in vectors it is not so easy to delete from the middle for example.My point is that when i call function Delete_Question() to delete him from the container. But because all my questions should have number ( 1...N-when loading the file and when adding new ones) i am not sure that std::vector is good choice.
The whole part should be like that:
Select question from [1..N]: "user input";

If(user input>N)
{
  cout<<"error message"
}
else
{
//here i have an swich case menu wiht Edit or Delete of that question
}

when you choose the Delete you should delete the question from the container.
After that you return to main menu again i did it with switch and there choose Save questions to file (probably here i will write in the file and if there is any data it will be erased) but this "number of question" of all the questions after erased one should decrease with 1 not to stay the same.

Indeed, deleting from a memory-contiguous-like container (such as a vector, or an array), requires a bit of time, because the system has to shift all the elements to get the desired result, but, it has some advantages. I see you mention a lot that you want to access the quesitons by their given id. Vectors for example, have a direct access to its members, as opposed to linked lists.
If you think you'll be doing a lot of deleting from the middle, than I would suggest looking over linked lists, since their delete and insert time are rather closed to O(1). As I said before, it's downside is the element access. You can't directly access elements from your liked-list, you have to parse the entire list, till you find the desired element.

Regarding your problem, I would suggest using std::vector since it's easier to handle, and with small amount of information (300-400 question), the deletion wouldn't be a problem. For deleting elements you should look into std::vector's function:
erase.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.