Exam Menu-Driven File I/O HELP!

Part 1:Create an examination question class hierarchy in a menu-driven program. Develop an exam class to load the exam from a file and display each question to the screen.
Part 2: Create a menu to the following:
1. Load an exam
2. Take an exam
3. Show exam results
4. Quit
Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file.
Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g., "Good job" or "Better luck next time") Upon completion of the exam, the program should return the user to the menu.
Choice 3: The program should display the total points available and the total points scored during that exam. A percentage score should also be displayed. (Optional: if you choose to track which problems were missed, you could display that information for the user.)
Choice 4: Exit the program display a goodbye message.
You should consider creating an additional class Student that will track student's score through methods such as addPointsPossible, addPointsScored, getPointsPossible, and getPointsScored. You should also enhance your Exam class to include methods getPointValue and getAnswer. You may also want to add a method to only display one question at a time, such as displayQuestion.

This is what I have so far but no matter what I change or how many variables I have created, I still keep getting error messages. PLEASE SOMEONE HELP! Also, I am using Dev-C++.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void displayMenu();
short getUserInput();
void exitMessage();


class Exam
{
    private:
        string fileName;
        string fileText;
        ifstream file;
    
    public:
        void loadExam();
        void displayExam();
    
};

int main()
{
    
    bool keepRunning = true;
    short userInput = 0;
    Exam exam;
    
    while(keepRunning)
    {
        displayMenu();
        userInput = getUserInput();
        
        
        if(userInput == 1)
        {
            exam.loadExam();
            
        }else if(userInput == 2)
        {
            exam.displayExam();
            
        }else
        {
            keepRunning = false;
        }
        
    }
    exitMessage();
    
    return 0;
}

void displayMenu()
{
    cout<<endl;
    cout<<"1) Load Exam"<<endl;
    cout<<"2) Display Exam"<<endl;
    cout<<"3) Show Results"<< endl;
    cout<<"4) Quit"<<endl;
    cout<<"\nEnter an option: ";
}

short getUserInput()
{
    short userInput = 0;
    cin>>userInput;
    
    while(userInput<1 || userInput>4)
    {
        cout<<"\nERROR: Option must be between 1 - 4"<<endl;
        cout<<"\nEnter an option: ";
        cin>>userInput;
    }
    
    cin.clear();
    cin.ignore(10000,'\n');
    return userInput;
}

void exitMessage()
{
    cout <<"\n\nThank you for visiting!"<<endl;
}

void Exam::loadExam()
{
    cout<<"\n-- Load Exam --"<<endl;
    cout<<"\nEnter file name: ";
    getline(cin,fileName);
    
    file.open();
    
    while(!file)
    {
        cout<<"\n    ERROR: File does not exist!"<<endl;
        cout<<"\n    Enter file name: ";
        getline(cin,fileName);
        
        file.open(fileName);
    }
    
    file.close();
    cout<<"\n    File successfully loaded!"<<endl;
    
}

void Exam::displayExam()
{
    
    cout<<"\n-- Display Exam --"<<endl;
    file.open(fileName);
    
    string line;
    while (getline(file, line))
    {
    	if(line.empty()) continue;
    	istringstream iss(line);
		int sum = 0, next = 0;
		while (iss >> next) sum += next;
		file << sum << endl;
	}
    
    if(file.is_open())
    {
        cout<<endl;
        while(getline(file,fileText))
        {
            cout<<fileText<<endl;
        }
        cout<<endl;
        file.close();
    }else
    {
        cout<<"\nERROR: Unable to open file!"<<endl;
    }
}
Last edited on
Topic archived. No new replies allowed.