For this assignment, practice using iostream, getline, and string ops. Your prog
ID: 3883673 • Letter: F
Question
For this assignment, practice using iostream, getline, and string ops. Your program should ask for a output filename.
It will read test1.csv.
test1.csv contains their firstname, lastname, birthday, and yearly income of 4 people formatted in the following way: firstname;lastname;mm/dd/yyyy;$yearlyIncome .
It will calculate the monthly income and age (in years) of each person and output that information to the provided output filename in the following format: lastname, firstname;age;$monthlyIncome.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<sstream>
#include<time.h>
using namespace std;
int main(){
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
int year = aTime->tm_year + 1900;
string fn, ln, dob, inc, file;
ifstream ip;
ip.open("test1.csv");
cout<<"Enter output file name:-";
getline(cin, file);
ofstream f;
f.open(file.c_str());
while(ip){
string yr;
int yri, yi, age, mi;
getline(ip,fn,',');
getline(ip,ln,',');
getline(ip,dob,',');
getline(ip,inc,' ');
yr = yr.append(dob,6,4);
stringstream ss(yr);
ss >> yri;
stringstream si(inc);
si >> yi;
age = year - yri;
mi = yi/12;
f << ln <<","<< fn <<","<< age <<","<< mi <<" ";
}
f.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.