Problem while running my c++ project

hello i’m new here please could you help me withh this problem btw i included all the libraries necessary idk why they are not shown in the post i get this error terminate called after throwing an instance of ‘std::out_of_range’
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
this is my code :

#include<iostream>

#include<fstream>

#include <string>

#include "Qcm.h"

#include "Question.h"

#include <sstream>

using namespace std;

void AfficherQuestion(int d,Question & Q,std::fstream  &Quiz,Qcm & qcm1,int i){

    std::string line ;

     for(int j=0;j<(d+6);j++){

            getline(Quiz,line);

            if(j==d){ Q.setenoncee(line);cout<<line<<endl;}

            if(j<=(d+4) && j>=(d+1)){

                string choix1;

                string choix2;

                string choix3;

                string choix4;

                if(j==d+1){ choix1=line; cout<<line<<endl;}

                if(j==d+2){ choix2=line;cout<<line<<endl;}

                if(j==d+3){ choix3=line;cout<<line<<endl;}

                if(j==d+4){ choix4=line;cout<<line<<endl;

                     Q.setChoix(choix1,choix2,choix3,choix4);

                }

               

            }

            if(j==d+5){

                stringstream ss;

                ss<<line;

                int r;

                ss>>r;

                Q.setreponse (r);//convert string to int

                int a;

                do{

                    cout<<"entrez votre reponse en entrant le nbr de choix "<<endl;

                    cin>>a;

                }while(a<1||a>4);

                qcm1.fillAnswers(a);

                qcm1.verifyAnswer(i);

                               

            }

                           

        }

       

}

int main(){

   

    fstream Quiz("Quiz.txt",ios::in);

    //Quiz.open("Quiz.txt",ios::in);

    Qcm qcm1;

    int d=0;

    for(int i=0;i<=40;i++){

        Question Q;

        if(d==0){

            AfficherQuestion(d,Q,Quiz,qcm1,i);

            d=d+6;

   

        }else{

           

            AfficherQuestion(d,Q,Quiz,qcm1,i);

            d=d+6;

        }

        /*for(int j=0;j<=8;j++){

            string line ;

            getline(Quiz,line);

            if(j==0){ Q.setenoncee(line);cout<<line<<endl;}

            if(j<5 && j>=1){

                string choix1;

                string choix2;

                string choix3;

                string choix4;

                if(j==1){ choix1=line; cout<<line<<endl;}

               

                if(j==2){ choix2=line;cout<<line<<endl;}

                if(j==3){ choix3=line;cout<<line<<endl;}

                if(j==4){ choix4=line;cout<<line<<endl;}

                Q.setChoix(choix1,choix2,choix3,choix4);

            }

            if(j==5){

                stringstream ss;

                ss<<line;

                int r;

                ss>>r;

                Q.setreponse (r);//convert string to int

            }    

        }*/

       // Q.Afficher();

        qcm1.enregisterQuestion(Q);

        if(i==40){

            cout<<"score is :"<<endl;

            cout<<qcm1.getscore();

        }

    }

   

    return 0;

}
//un compteur qui compte ou on a s'arreter en fichier function qetquestion

#ifndef QCM_H // include guard

#define QCM_H

#include <iostream>

#include<string>

#include "Question.h"

#include <vector>

class Qcm{

    private:

         int  score=0;

        std::vector<Question> Questions;

        std::vector<int> Answers ;//it will choose just the number of the choice which he think it is the correct answer

    public:

       void enregisterQuestion(Question Q){ Questions.push_back(Q);}

       void fillAnswers(int a){Answers.push_back(a) ; }

       void verifyAnswer(int i){

        if(Answers.at(i)==Questions.at(i).getresponse()){

           

            score=score+1;

           }

       

       }

       int getscore(){

            return score;

       }

           

};

#endif
#ifndef QUESTION_H // include guard

#define QUESTION_H

#include <iostream>

#include<string>

class Question{

    private:

        std::string enoncee;

        std::string choix[4] ;

        int response;

        //std::string answer;

    public:

        Question(std::string,std::string[],int);

        Question();

        void Afficher();

        /*bool getanswer(){

           return (answer==choix[response]);

        }*/

        void setenoncee(std::string enonce){ enoncee=enonce;}

        void setreponse(int r){response=r;}

        int getresponse(){return response;  }

        void setChoix(std::string choice1, std::string choice2,std::string choice3,std::string choice4 ){choix[0]=choice1 ;choix[1]=choice2 ;choix[2]=choice3 ;choix[3]=choice4;}

};

#endif
#include"Question.h"

#include<string>

#include<iostream>

using namespace std;

Question::Question(string eno,string choice[4],int reponse){

    enoncee=eno;

    for(int i=0;i<4;i++){

        choix[i]=choice[i];

    }

    response=reponse;

}

Question::Question(){

    enoncee="null";

    for(int i=0;i<4;i++){

        choix[i]="null";

    }

    response=0;

}

void Question::Afficher(){

    cout<< enoncee<<endl;

    cout<< choix[0]<<endl;

    cout<< choix[1]<<endl;

    cout<< choix[2]<<endl;

    cout<< choix[3]<<endl;

}

Hi @ZAINAB,

I’m not sure how this relates to ROOT, but anyway: it seems that you might be trying to access an empty std::vector, presumably either Answers or Questions.

Cheers,
J.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.