Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a c++ program that is menu-driven that allows ... Question: Write a C++ pr

ID: 3869171 • Letter: W

Question

write a c++ program that is menu-driven that allows ... Question: Write a C++ program that is menu-driven that allow... Write a C++ program that is menu-driven that allows for the following menu choices: Load an exam: Loading an test should prompt the user for an test file. If no file exists, it should allow the user to specify a different file. Upon a successful load of the test, the user should be presented with the menu again. Display exam: The program should simply display each question, its point value, and the answer to the screen. (The functionality of actually taking the test will be created in Week 5). Upon displaying the test to the screen, the user should be presented with the menu again. Quit: Quit the program gracefully by displaying a "thank you" message to the user, and ensure that all files have been closed along with any other housekeeping that should be done as your program shuts down. Consider creating a class test that will hold the actual test and provide behavior such as loadTest and also displayTest. This class will be enhanced in Week 5.

Explanation / Answer

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<fstream>

using namespace std;

struct question {
string text;
int point_value;
string answer;
};

class Test{

   private:
        vector<question> list; // holding the questions in the file.

   public:
        void fill_data(){
            ifstream fin;
            string line;
            int count;
            question q;

            fin.open("input5.txt"); // Assuming the input file is "input5.txt .As test file format is not given ,the input file format assumed is, first line is                                    
                                    //is the question text,Second line is the point value, third line is the
                                     //answer and then format repeats from the second
                                    //line onwards. Questions are separated by blank line
            if (!fin){
                cout << "Error in opening file ";
                return;
            }
           
            while (getline(fin,line))
            {
                if (line.size() == 1){
                   continue;
                }
                q.text = line;
                getline(fin,line);
                istringstream iss1(line);
                iss1 >> q.point_value;
                getline(fin,q.answer);
                list.push_back(q);
                                 
            }

            fin.close();
         }

        void display(){
           for (int i = 0; i<list.size(); i++){
                cout << list[i].text << endl;
                cout << list[i].point_value << endl;
                cout << "Answer:" << list[i].answer << endl;
                cout << endl;
           }
        }

  
};


int main(){

   Test ct;
   int choice;

  
  
   do {
        cout <<"1.Load an exam" << endl;
        cout <<"2.Display exam" << endl;
        cout <<"3.Quit" << endl;
        cout << "Enter your choice: " << endl;
        cin >> choice;

        switch(choice) {
              case 1:
                     ct.fill_data();
                     break;
              case 2:
                     ct.display();
                     break;
        }

   } while (choice != 3);
      
   return 0;
}