Write a C++ program that is menu-driven that allows for the following menu choic
ID: 3847082 • Letter: W
Question
Write a C++ program that is menu-driven that allows for the following menu choices: Load an exam: Loading an exam should prompt the user for an exam file. If no file exists, it should allow the user to specify a different file. Upon a successful load of an exam, 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 exam will be created in Week 5). Upon displaying the exam 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 exam that will hold the actual exam and provide behavior such as loadExam and also displayExam
Explanation / Answer
#include <iostream>
using namespace std;
#include<fstream>
class exam
{
string q; //question
float p; //points
string a; //answer
public:
exam(string ques=" ", float points=0, string answer=" ")
{
q=ques;
p=points;
a=answer;
}
void print()
{
cout<<" Ques: "<<q;
cout<<" Points: "<<p;
cout<<" Answer: "<<a;
}
};
int main(int argc, const char * argv[])
{
int ch;
ifstream fin;
string fn;
exam temp;
while(1)
{
cout<<" 1. Load file 2. Display file 3. Quit: ";
cin>>ch;
switch (ch)
{
case 1:
cout<<"Enter file name: ";
cin>>fn;
fin.open(fn,ios::in|ios::binary);
if(!fin)
cout<<"Unable to open file ";
break;
case 2:
while(fin.read((char*)&temp,sizeof(temp)))
{
temp.print();
}
default:
cout<<"Thank You";
fin.close();
return 0;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.