Hello all, I am having a really tough time figuring out how to structure the input of one .dat file and outputting into another one. I have to loop through a file structured "doe, john, 1500", check for errors and then increase the mp by 50. You also have to do this through using a person class too. I think I need to use a while loop to go to eof and use getline to input correctly, but im not sure how to structure it. Any help would be fantastic, I am pretty new to C++ and I feel like I am floundering on this one.
Data file:
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
person class
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)
{}
person(const std::string& init_fn,
const std::string& init_ln,
float 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;}
float get_monthly_pay() const {return monthly_pay+50.00;}
};
#endif
and then here is my main
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "person class.h"
int main() {
std::ifstream input("person.dat");
if (!input) {
std::cerr << "cannot open file" << std::endl;
return 1;
}
person in;
input >> in.last_name;
if (input.eof()) {
std::cerr <<"no data in file" << std::endl;
return 2;
}
if (input.fail() || input.peek() != ',' || !input.ignore()) {
std::cerr <<"last name error" << std::endl;
return 3;
}
if (!getline(input, in.first_name, ',')) {
std::cerr <<"first name error" << std::endl;
return 4;
}
input >> in.monthly_pay;
if (!input) {
std::cerr <<"error with pay" << std::endl;
return 5;
}
std::ofstream stream ("output.dat");
stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay << "/n"<<std::endl;
return
0;
}