Create a problem in C++: Design a program that will read in 2 files (“Cars.txt”
ID: 3852026 • Letter: C
Question
Create a problem in C++:
Design a program that will read in 2 files (“Cars.txt” and “detailReport.txt”) and then will print out the output that correspond to the car’s details giving in “detailReport.txt'
Here is a general skeleton of what you should have:
(Note: you can use string and vector for the program)
- A Structure ErrorList that have 2 variables errorCode (int) and errorMessage (string);
- A Class Car that have 4 private variables (name (string), errorSize (int); error (int *), numOfCar (static int)). (You have to make error a int* since each car have random amount of error and depend on errorSize). You need atleast constructor, destructor, mutators and accessors (get and set for each variable). You can add more functions if it need be.
- In main()
Read in “detailReport.txt” to an array of ErrorList.
Read in “Cars.txt” into an array of Car.
Once you have all the data into correct places in the program, do exceptional handling to print out each car with its errors (and throw “Car is in good shape” if Error is 0).
Tips:
When you read “Cars.txt” into program, be careful how you handle the numbers of errors for each car. Since each car have random amount of errors, if you just use a loop to read, it might crash your program; therefore you could read in as a string of words, then split the string into array of vector. After that set each element of the vector into correct location.
Explanation / Answer
The following code has not been tested in runtime due to lack of time but it doesn't contain any compilation error. Following assumptions are made for this code:
detailReport.txt looks like :
1 errordefinition1
2 errordefinition2
and Cars.txt looks like:
Mustang 1 2
BMW 2
O/p will look like
Mustang:
errordefinition1
errordefinition2
BMW:
errordefinition2
In case of any doubt please comment on the answer.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <stdio>
using namespace std;
class Car{
private:
string name;
int errorSize;
int* error;
static int numOfCar;
public:
Car();
~Car();
string getName();
void setName(string carName);
int getErrorSize();
void setErrorSize(int carErrorSize);
int* getError();
void setError(int* carError);
int getNumOfCar();
void setNumOfCar(int numCars);
int incementNumOfCars();
};
string Car::getName(){
return name;
}
void Car::setName(string carName){
name=carName;
}
int Car::getErrorSize(){
return errorSize;
}
void Car::setErrorSize(int carErrorSize){
errorSize=carErrorSize;
}
int* Car::getError(){
return error;
}
void Car::setError(int* carError){
error=carError;
}
int Car::getNumOfCar(){
return numOfCar;
}
void Car::setNumOfCar(int numCars){
numOfCar=numCars;
}
int Car::incementNumOfCars(){
return ++numOfCar;
}
struct ErrorList{
int errorCode;
string errorMessage;
};
vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
int main() {
//Reading detailReport.txt
fstream file;
file.open("detailReport.txt",ios::in);
vector <ErrorList> errorCodeDetails;
string line;
while(getLine(file,line)){
vector<string>temp=split(line,' ');
ErrorList eList;
eList.errorCode=stoi(temp[0]);
eList.errorMessage=temp[1];
errorCodeDetails.push_back(eList);
}
file.close();
file.open("Cars.txt",ios::in);
vector <Car> carDetails;
while(getLine(file,line)){
vector<string>temp=split(line,' ');
Car car;
car.incementNumOfCars();
car.setErrorSize(temp.size()-1);
int* errors=new int[car.getErrorSize()];
car.setName(temp[0]);
for(int i=1;i<temp.size();i++){
errors[i-1]=stoi(temp[i]);
}
carDetails.push_back(car);
}
vector<Car>::iterator carIterator = carDetails.begin();
while(carIterator!=carDetails.end()){
cout<<(*carIterator).getName()<<":"<<endl;
if((*carIterator).getErrorSize()==0){
cout<<"Car is in good shape."<<endl;
}else{
for(int i=0;i<(*carIterator).getErrorSize();i++){
vector<ErrorList>::iterator errorIterator = errorCodeDetails.begin();
while(errorIterator!=errorCodeDetails.end()){
if((*carIterator).getError()[i]==(*errorIterator).errorCode){
cout<<(*errorIterator).errorMessage<<endl;
}
}
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.