Needs to be written in C++ skeleton on main: int main(int argc, char** argv) { /
ID: 3721804 • Letter: N
Question
Needs to be written in C++
skeleton on main:
int main(int argc, char** argv)
{
//Do a check to make sure we have the right number of arguments,
//exit if there aren't enough arguments
std::string fileName = ???;//get the file name from argv
DMV myDMV(fileName);
myDMV.run();
}
Information in a file that the program needs to read:
Requirements What's the most exciting thing you can think of? Exactly! The DMV! Today we're going to write a program that reads in Drivers License Records from file and then let's the user interact with the data. Data File Here is the data file you will read from: linka, It is formatted in the following way: Sample from file: Francine Palau 23 N 381043 Kam Swindler 57 Y 449122 Migdalia Constable 21 Y 401934 Classes DriversLicenseRecord Class • Design a Drivers License Record class • Note that a Driver has a first name, last name, age, voter status, and license number • All member variables should be private • Create getters and setters as they are needed . NOTE: This class only contains data, it doesn't not interact with the user! DMV Class The DMV class will be in charge of... • Constructor • Reading and storing the drivers license records • Make sure the file exist! . Interacting with the user · Print menus • Validate user input! • All VO to the terminal Here's the menu for the user: Select an option: 1) print all Drivers Info 2) Print all voters 3) Print drivers by laat initial 4) Print drivers in age range 5) Quit Enter your choice: Option Description Prints all drivers and all their information in the following format: , ( ): Print all Driver Info Example of a single entry (you'll print all entries) Johnson, Larry (67): 301288 Print all voters Prints the driver information in the same format as the previous option, but only prints those registered to vote. Print drivers by last initial Asks the user for single character, and print the information for all drivers that have a last name starting with that letter (case-insensitive) OR print "No record found." if no drivers have a last name starting with that character Print drivers in age range Prompts the user for two ints that represent an age range. Print all drivers (following the same format as when printing 1 driver) within this age range Quit Exits the program.Explanation / Answer
//main.cpp
#include"DMV.h"
#include"DriversLicenseRecord.h"
#include<fstream>
using namespace std;
int main(int argc, char** argv)
{
if(argc<2)
{
cout<<"You didn't specify an input file name! please try again ";
return 0;
}
string fileName = argv[1];
ifstream fileIn;
fileIn.open(fileName.c_str());
if(!fileIn.is_open())
{
std::cout<<"The file did not exist! please try again ";
}
fileIn.close();
DMV myDMV("");
myDMV.run();
return 0;
}
------------------------------------------------------------------------------------------
//DMV.cpp
#include "DMV.h"
using namespace std;
DMV::DMV(string fileName)
{
record = new DriversLicenseRecord(fileName);
}
DMV::~DMV()
{
delete record;
}
void DMV::printMenu()
{
std::cout<<"Select an option: "
<<"1) Print all Drivers Info "
<<"2) Print specific driver "
<<"3) Create registered voter file "
<<"4) Print in ascending order in age range "
<<"5) Quit "
<<"Enter your choice: ";
}
void DMV::run()
{
int choice = 0;
do
{
printMenu();
cin>>choice;
if(choice == 1)
{
record->printAllInfo();
}
else if(choice == 2)
{
cout<<"Enter a drivers license number: ";
int tempNum;
cin>>tempNum;
record->printDriver(tempNum);
}
else if(choice == 3)
{
string fileName;
cout<<"Enter a file name to create a record in: ";
cin>>fileName;
record->createVoterFile(fileName);
}
else if(choice == 4)
{
int min;
int max;
cout<<"Enter a minumum value for ages: ";
cin>>min;
cout<<"Enter a maximum value for ages: ";
cin>>max;
record->printAscendingAges(min,max);
}
}while (choice!=5);
}
----------------------------------------------------------------------------------
//DMV.h
#include "DriversLicenseRecord.h"
#ifndef DMV_H
#define DMV_H
using namespace std;
class DMV
{
public:
DMV(string fileName);
~DMV();
void run();
private:
DriversLicenseRecord* record;
void printMenu();
};
#endif
--------------------------------------------------------------------------
//DriversLicenseRecord.cpp
#include "DriversLicenseRecord.h"
using namespace std;
DriversLicenseRecord::DriversLicenseRecord(string fileName)
{
ifstream fileIn(fileName);
fileIn>>numEntries;
allVoters = new voter[numEntries];
for(int i=0; i<numEntries; i++)
{
voter newVoter;
fileIn >> newVoter.firstName;
fileIn >> newVoter.lastName;
fileIn >> newVoter.age;
fileIn >> newVoter.registrationStatus;
fileIn >> newVoter.licenseNumber;
allVoters[i] = newVoter;
}
fileIn.close();
}
DriversLicenseRecord::~DriversLicenseRecord()
{
delete[] allVoters;
}
void DriversLicenseRecord::printAllInfo()
{
for(int i=0; i<numEntries; i++)
{
string curLast = allVoters[i].lastName;
string curFirst = allVoters[i].firstName;
int curAge = allVoters[i].age;
int curNum = allVoters[i].licenseNumber;
cout<< curLast << ", " << curFirst << " (" << curAge << ") " << curNum << " ";
}
std::cout<<" ";
}
void DriversLicenseRecord::printDriver(int licenseNum)
{
int i = checkDriverExists(licenseNum);
if (i < 0)
{
cout<<"No record was found for that license number ";
}
else
{
string curLast = allVoters[i].lastName;
string curFirst = allVoters[i].firstName;
int curAge = allVoters[i].age;
int curNum = allVoters[i].licenseNumber;
cout<< curLast << ", " << curFirst << " (" << curAge << ") " << curNum << " ";
}
}
void DriversLicenseRecord::createVoterFile(string fileName)
{
ofstream fileOut(fileName);
for(int i=0; i<numEntries; i++)
{
if(allVoters[i].registrationStatus == 'Y')
{
fileOut << allVoters[i].firstName << " ";
fileOut << allVoters[i].lastName << " ";
fileOut << allVoters[i].age << " ";
fileOut << allVoters[i].registrationStatus << " ";
fileOut << allVoters[i].licenseNumber << " ";
}
}
fileOut.close();
}
int DriversLicenseRecord::checkDriverExists(int licenseNum)
{
for(int i=0; i<numEntries; i++)
{
if(allVoters[i].licenseNumber == licenseNum)
{
return i;
}
}
return -1;
}
void DriversLicenseRecord::printAscendingAges(int min, int max)
{
int size = 0;
for(int i=0; i<numEntries; i++)
{
if(allVoters[i].age >= min && allVoters[i].age <= max)
{
size++;
}
}
if(size == 0)
{
std::cout<<" No voters were within that age range ";
return;
}
else
{
vector<voter> voterList;
for(int i=0; i<numEntries; i++)
{
if(allVoters[i].age >= min && allVoters[i].age <= max)
{
voterList.push_back(allVoters[i]);
}
}
sortByAges(voterList);
for(int i=0; i<voterList.size(); i++)
{
string curLast = voterList[i].lastName;
string curFirst = voterList[i].firstName;
int curAge = voterList[i].age;
int curNum = voterList[i].licenseNumber;
cout<< curLast << ", " << curFirst << " (" << curAge << ") " << curNum << " ";
}
}
}
void DriversLicenseRecord::sortByAges(vector<voter> &list)
{
voter temp;
for(int i=0; i<list.size()-1; i++)
{
for(int j=0; j<list.size()-1; j++)
{
if(list[j].age > list[j+1].age)
{
temp = list[j+1];
list[j+1] = list[j];
list[j] = temp;
}
}
}
}
-----------------------------------------------------------------------------------------------------
//DriversLicenseRecord.h
#ifndef DRIVERSLICENSERECORD_H
#define DRIVERSLICENSERECORD_H
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class DriversLicenseRecord
{
public:
DriversLicenseRecord(string fileName);
~DriversLicenseRecord();
void printAllInfo();
void printDriver(int licenseNum);
void createVoterFile(string fileName);
void printAscendingAges(int min, int max);
private:
struct voter{
string firstName;
string lastName;
int age;
char registrationStatus;
int licenseNumber;
};
int checkDriverExists(int licenseNum);
void sortByAges(vector<voter> &list);
voter* allVoters;
int numEntries;
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.