I just started studying files in C ++. I was trying to do an exercise but I have not been able to get it out.
The statement is as follows:
Perform a program that reads the name of a file by keyboard. This file will contain a series of test questions. The number of questions that the file will contain will never be greater than 50.
For each question, the file will contain three possible answers and a number indicating which of the three possible answers is correct (see attached example file). In this way, the format of the file will be as follows:
pregunta1 --salto de linea respuesta_correcta_a_la_pregunta1 salto de linea respuesta1 salto de linea respuesta2 salto de linea respuesta3 salto de linea pregunta2 salto de linea respuesta_correcta_a_la_pregunta2 salto de linea respuesta1 salto de linea respuesta2 salto de linea respuesta3 salto de linea ...
The program, after loading the data from the file into memory in an appropriate data structure, should perform the test by displaying each of the questions and possible answers on the screen and reading the answer to each question on the keyboard.
At the end, the program will display on the screen the answer number correctly answered, the number of answers incorrectly answered and the score calculated according to the following formula:
puntuación = 10 * (respuestas_correctas – (respuestas_incorrectas / 2)) / total_de_preguntas_en_el_fichero
Keep in mind that the score should be calculated as a real number.
I leave you my code:
#include <iostream>
#include <fstream>
#include <array>
#include <string>
using namespace std;
const int MAX_PREG = 10;
struct preguntas{
string pregunta;
int respuesta;
string respuesta1;
string respuesta2;
string respuesta3;
};
typedef array <preguntas,MAX_PREG> array_dat;
struct Ttest{
int np = 0;
array_dat pr;
};
int main(){
int respuesta_introducida;
int contador = 0;
Ttest x;
array_dat y;
string fichero;
cout << "Introduce el nombre del fichero " << endl;
cin >> fichero;
ifstream fichero1;
fichero1.open(fichero);
string linea;
int correcta;
//getline(fichero1,linea);
//contador++;
while(!fichero1.eof()){
if (contador == 0 || contador % 4 == 0){
x.pr[x.np].pregunta = linea;
}
if (contador == 1 || contador % 1 == 1){
x.pr[x.np].respuesta = correcta;
}
if (contador == 2 || contador % 2 == 0){
x.pr[x.np].respuesta1 = linea;
}
if(contador == 2 || contador % 2 == 0){
x.pr[x.np].respuesta2 = linea;
}
if(contador == 3 || contador % 3 == 0){
x.pr[x.np].respuesta3 = linea;
}
getline(fichero1,linea);
}
}
The example txt I was using was this:
De los siguientes animales cual no es un mamifero? 2 La ballena El pulpo La vaca Cuando vale 0 dividido por 5? 1 0 1 No se puede calcular Un bucle for 2 Puede utilizar una variable de control de tipo double Tiene que ser determinista Ninguna de las otras dos respuestas es correcta
The doubts I have is that I do not know how to assign each line to each element of the structure, that is to say, to choose the line that I assign to each element of the structure. From that arises the mess that I have below with the counters, etc