Help random vector...

Hey! I need help .. Im stuck with my program im working with.. It's designed to create questions and cout questions in random order... I dont know how to use random with questions instead of numbers.. Help please!

#ifndef Crandom_h_
#define Crandom_H_

#include <string>

class CRandom
{
public:
// grundstruktur för ge ut random.

int randomize(int min, int max);
void seed();

private:



};


#endif


--------------------------------


int main()
{
vector<CÄmne>Ämnet;
vector<CFrågor>Questvector;
queue<CFrågor>GQuest;
vector<CFrågor>VGQuest;
vector<CFrågor>MVGQuest;

CÄmne Pemt;
CFrågor temp;
string str;
string ämne;
char betyg;

cout << "Vilket Ämne väljer du?" << endl;
cin >> ämne;
cin.ignore(1, '\n');

Pemt.setÄmne(ämne);

Ämnet.push_back(Pemt);


while(1)
{
cout << "type question and type "klar" when ur done" << endl;
getline(cin, str);

if(str != "klar")
{
cout << "What grade?" << endl;
cin >> betyg;
cin.ignore(1, '\n');
}

//CRandom random;

//int index = random.randomize(0, Gfrågor.size());

system("cls");

if(str == "klar") // om du matar in klar då avbryts ditt skrivande.
break;

temp.setFrågor(str);
temp.setBetyg(betyg);

if(betyg == 'G')
{
Gfrågor.push(temp);

}

if(betyg == 'V')
{
VGfrågor.push_back(temp);
}

if(betyg == 'M')
{
MVGfrågor.push_back(temp);
}

}



for(int i = 0; i < Gfrågor.size(); i++)
{

CFrågor temp = Gfrågor.front();

//int index = random.randomize(1, Gfrågor.size());
cout << "Betyg: " << temp.getBetyg() << " " << endl;
cout << "Fråga: " << temp.getFrågor() << endl;

Gfrågor.pop();

}

return 0;
}
Do you have your questions in something like a vector list or a list of some form? Can you get the count of that list? Can you randomize the a number in the range of 0 to max of the list? can I extract something from that list at the random position? Last time I looked vector<> and list<> both had things I could do like this.
Im just using vector in my class CFrågor. like this below.

#include <string>
#include <iostream>
#include <locale>


class CFrågor
{
public:

void setFrågor(std::string frågor) { m_frågor = frågor;}
std::string getFrågor() { return m_frågor;}

void setBetyg(char Betyg) { m_betyg = Betyg;}
char getBetyg() { return m_betyg;}

private:

std::string m_frågor;
char m_betyg;
int m_index;

};
I don't know what language it is but I am assuming CFragor is the Question class, which is put on the vector<CFragor> myFragors; // list of questions .

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
30
31
32
33
#include <string>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

clase CFragor; /// just a prototype...

int main()
{
      std::vector<CFragor> myFragors;
      
      // load my questions up....

      int nCount = myFlagors.size();
      std::srand(time(NULL));

      int nPos = (std::rand() % nCount) -1; // the random position in the vector list, which is 0 based.

      std::cout << myFragors[nPos].getFragor() << std::endl;
      
      std::string Betyg;
      std::getline(cin, Betyg);
      if(Betyg == myFragors[nPos].getBetyg())
      {
            std::cout << "Your answer was right!!" << std::endl;
      }
      else
      {
            std::cout << "Your Answere was wrong" << std::endl;
      }
     return 0;
}
Last edited on
Topic archived. No new replies allowed.