I am a beginner C++ programmer and I am having a lot of difficulty with this assignment for my class. I am inputing file data from person.dat and outputting to output.dat. I have to have error checking throughout, I am getting errors and I am not sure how to get around the problem. Any help would be great
person.dat file contents:
Horowitz,Jake,1200.24
Flabitz,Bubba,1712.38
Blackwell,Sharon,1872.93
Molerat,Rufus,501.00
Goodall,Jane,1567.43
Sommerset,William,1359.57
Van Helsing,Marcus,1675.21
Welfirth,Bula,1903.83
Poling,Kerri,1243.32
Lee,Aswari Kalim,1839.42
Lisa,Mona,2409.48
Lisbon,Frieda,1543.70
Hatcher,Gary,1220.13
Code:
#pragma once
#if !defined(person_included)
#define person_included
#include <string>
#include <iostream>
class person{
public:
std::string first_name;
std::string last_name;
double monthly_pay;
person():
first_name ("john"),
last_name ("doe"),
monthly_pay (0.00)
{}
person(const std::string& init_fn,
const std::string& init_ln,
double init_mp
):first_name(init_fn),
last_name(init_ln),
monthly_pay (init_mp)
{}
std::string get_first_name() const {return first_name;}
std::string get_last_name() const {return last_name;}
double get_monthly_pay() const {return monthly_pay+50.00;}
/*friend std::ostream& operator<<(
std::ostream& j,
const person& s)
{
j << s.last_name <<","<< s.first_name<<","<< s.monthly_pay<<"/n";
return j;}*/
};
#endif
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "person class.h"
int main() {
std::ifstream input("person.dat");
person in;
if (!input) {
std::cerr << "cannot open file" << std::endl;
return 6;
}
for (;;){
try{
if (input.eof()) {throw 1;}
if (!std::getline(input, in.last_name, ',')) {throw 2;}
if (!std::getline(input, in.first_name, ',')) {throw 3;}
input >> in.monthly_pay;
if (!input) {throw 4;}
std::ofstream stream ("output.dat");
stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
}catch(int error){
std::cerr <<"an error occured" << error <<std::endl;
}
}
return
0;
}