'How do I use read the output from a txt file and store the information into an array that keeps track of all question, and an array of answers

Im not sure if my function actually works correctly. Im trying to open a txt file and read its contents into a struct with an array of questions and for the array to hold the correct output answer that the txt file has in it. Im doing this for school and there is an existing code that is provided that gets run if the function works correctly but im not sure if the actual output from the txt file is being stored anywhere and how to access it.

I need to perform a validation on the format of the questions to see if its {mc} "multiple choice" or {ma} "multiple answer" and I dont know how I can do this validation because I'm not sure how to store the output correctly and in what way to validate those outputs. Im sorry if im not explaining this clearly or well Im still learning and there is a lot that I dont understand. I would appreciate any pointers or reccomendation about what to do, because Im really stuck, thank you.

Im going to provide the code that I have and what I've done so far, I hope some sense can be made out of what Im trying to do.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "quiz.h"
using namespace std;
using namespace quizzer;

// this is the code that I need to build my program around

int main()
{
 // USAGE CASE #1: load a test from a file that doesn't exist
 cout << "------------------------------------------------------------\n";
 cout << "USAGE CASE #1: bad input file\n";
 cout << "------------------------------------------------------------\n";
 if (quizzer::LoadQuiz("quiz1.txt") == 0)
 {
    cout << "ERROR: There is no file; the function 'LoadTest()' should return non-zero 
  value.\n";
    cout << "       Keep working on your code.\n";
 }
 else
 {
    cout << "SUCCESS\n";
 }
 cout << "------------------------------------------------------------\n\n";
 /*
 // USAGE CASE #2: load a valid quiz from the file and display it to the user
 cout << "------------------------------------------------------------\n";
 cout << "USAGE CASE #2: good quiz\n";
 cout << "------------------------------------------------------------\n";
 if (quizzer::LoadQuiz("quiz2.txt") == 0)
 {
    if (quizzer::IsQuizValid() == 1)
    {
        // This function should produce no output since the user didn't take the quiz 
     yet
        quizzer::ShowQuizResults();
        cout << "****** Answer [2] to every question. *****\n";

        int i = 0;
        do
        {
            cout << "\nQUESTION [" << ++i << "]\n";
            cout << "^^^^^^^^^^^^\n";
            quizzer::ShowNextQuestion();
        } while (quizzer::HasMoreQuestions());

        // The quiz has finished; show the user's score (each question is worth 1 point)
        quizzer::ShowQuizResults();
    }
    else
    {
        cout << "ERROR: The quiz should be valid, but the program is not detecting it as 
     valid.\n";
        cout << "       Keep working on your code.\n";
    }
}
cout << "------------------------------------------------------------\n\n";


// USAGE CASE #3: load an invalid quiz from the file
cout << "------------------------------------------------------------\n";
cout << "USAGE CASE #3: bad quiz (not enough questions)\n";
cout << "------------------------------------------------------------\n";
if (quizzer::LoadQuiz("quiz3.txt") == 0)
{
    if (quizzer::IsQuizValid() == 1)
    {
        cout << "ERROR: The file contains an invalid quiz, yet the program doesn't 
     detect it as invalid.\n";
        cout << "       Keep working on your code.\n";
    }
    else
    {
        cout << "SUCCESS\n";
    }
    // This function should produce no output since the quiz is not valid
    quizzer::ShowQuizResults();
  }
  cout << "------------------------------------------------------------\n\n";


  // USAGE CASE #4: load another valid quiz from the file and display it to the user
  cout << "------------------------------------------------------------\n";
  cout << "USAGE CASE #4: another good quiz\n";
  cout << "------------------------------------------------------------\n";
  if (quizzer::LoadQuiz("quiz4.txt") == 0)
  {
    if (quizzer::IsQuizValid() == 1)
    {
        // This function should produce no output since the user didn't take the quiz 
         yet
        quizzer::ShowQuizResults();
        cout << "****** Answer [3] to every question. *****\n";

        int i = 1;
        do
        {
            cout << "\nQUESTION [" << i++ << "]\n";
            cout << "^^^^^^^^^^^^\n";
            quizzer::ShowNextQuestion();
        } while (quizzer::HasMoreQuestions());

        // The quiz has finished; show the user's score (each question is worth 1 point)
        quizzer::ShowQuizResults();
    }
    else
    {
        cout << "ERROR: The quiz should be valid, but the program is not detecting it as 
    valid.\n";
        cout << "       Keep working on your code.\n";
    }
}
cout << "------------------------------------------------------------\n\n";



// USAGE CASE #5: load an invalid quiz from the file
cout << "------------------------------------------------------------\n";
cout << "USAGE CASE #5: another bad quiz (bad questions)\n";
cout << "------------------------------------------------------------\n";
if (quizzer::LoadQuiz("quiz5.txt") == 0)
{
    if (quizzer::IsQuizValid() == 1)
    {
        cout << "ERROR: The file contains an invalid quiz, yet the program doesn't 
     detect it as invalid.\n";
        cout << "       Keep working on your code.\n";
    }
    else
    {
        cout << "SUCCESS\n";
    }
    // This function should produce no output since the quiz is not valid
    quizzer::ShowQuizResults();
}
cout << "------------------------------------------------------------\n\n";
*/ 
return 0;
}

// and this is what Ive done so far in a module called quiz.cpp

#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <string>
#include "quiz.h"

namespace quizzer {
const int max_num_questions = 60;
const int max_answers = 10;
const int max_question_chars = 1024;
const int max_answers_chars = 128;
 // const char* filename = "quiz.txt";

struct Question {
    int question;
    char answer[256];
    char typeOfQuestion[max_num_questions];
    char  textOfQuestion[max_answers];
    int possibleAnswers;
    int correctAnswers;
   };
 struct Quiz {
    Question* questions;
    int num_questions;
    char quizQuestions[max_num_questions];
    int userScore;
    int shownQuestions;
    int remainingQuestions;
    char quizTaken;
 };
  int LoadQuiz(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        return 0;
    }
    Quiz quiz;
    // read the number of questions in the file
    fscanf(file, "%d\n", &quiz.num_questions); 
    // allocate memory for the questions
    quiz.questions == malloc(quiz.num_questions * sizeof(Question));
    // read each question from the file
    for (int i = 0; i < quiz.num_questions; i++) {
        fscanf(file, "%d %[^\n]\n", &quiz.questions[i].question,
            quiz.questions[i].answer);
    }
    fclose(file);

    return 1;
   }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source