Hello everyone! I am currently reading in a table separated by spaces which looks like this:
1 2 3 4
Pol1 M N 20 100000 1 .04 99
Pol2 F S 30 100000 1 .05 99
Pol3 M S 72 750000 1 .03 99
Pol4 F N 45 1000000 1 .05 99
However I would like to alter the table so that instead of it being separated by spaces it is separated by only commas. So the new table looks like this:
double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};
double ratesms[86] = {
#include "MaleSmoker.txt"
- 1
};
double ratesfn[86] = {
#include "FemaleNonsmoker.txt"
- 1
};
double ratesfs[86] = {
#include "FemaleSmoker.txt"
- 1
};
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
usingnamespace std;
#define STARTAGE 15
int main() {
//cout.setf(ios::fixed);
//cout.setf(ios::showpoint);
//cout.precision(2);
double tpx[100];
double myrate[100];
double T[100];
double vT[100];
double px[100];
double ax[100];
doubleconst *rates;
string line;
string age, death_ben, interest, end_age;
string policy, gender, smoker_status, db_option;
ifstream datafile("Policies.txt");
if (datafile.is_open()){
ofstream a_file("output\\output.txt");
a_file.precision(2);
a_file.setf(ios::fixed);
a_file.setf(ios::showpoint);
while (!datafile.eof()){
if datafile.peek()
datafile >> policy >> gender >> smoker_status >> age >> death_ben >> db_option >> interest >> end_age;
int Age;
int DeathBen;
double Interest;
int endAge;
if (!(istringstream(age) >> Age)) Age = 0;
if (!(istringstream(death_ben) >> DeathBen)) DeathBen = 0;
if (!(istringstream(interest) >> Interest)) Interest = 0;
if (!(istringstream(end_age) >> endAge)) endAge = 0;
if (gender == "M"){
if (smoker_status == "S"){
rates = ratesms;
}
else{
rates = ratesmn;
}
}
else{
if (smoker_status == "S") {
rates = ratesfs;
}
else{
rates = ratesfn;
}
}
double sum = 0;
tpx[Age] = 1;
T[Age] = 1;
vT[Age] = 1 / pow(1 + Interest, T[Age]);
while ((Age - STARTAGE) < endAge + 1 - STARTAGE){
//cout << "tpx for age " << Age << " is " << tpx[Age] << ".\n";
//cout << "vT for age " << Age << " = " << vT[Age] << "\n";
myrate[Age] = rates[Age - STARTAGE];
px[Age] = 1 - rates[Age - STARTAGE];
ax[Age] = myrate[Age] * tpx[Age] * vT[Age];
//cout << "qx for age " << Age << "= " << myrate[Age] << "\n";
//cout << "ax for age " << Age << " is " << ax[Age] << "\n";
Age = Age + 1;
tpx[Age] = tpx[Age - 1] * px[Age - 1];
T[Age] = T[Age - 1] + 1;
vT[Age] = 1 / pow(1 + Interest, T[Age]);
}
int otherAge;
if (!(istringstream(age) >> otherAge)) otherAge = 0;
for (int i = otherAge; i < 100; i++){
sum += ax[i];
}
cout << "Premium for " << policy << " is $" << sum*DeathBen << "\n";
a_file << "Premium for " << policy << " is $" << sum*DeathBen << "\n";
}
}
return 0;
}
I tried using this
1 2 3 4
while (!datafile.eof()){
getline(datafile, field, ',');
stringstream ss(field);
ss >> policy >> gender >> smoker_status >> age >> death_ben >> db_option >> interest >> end_age;
but it ended my while loop after only the first policy (only read in first row in table) and didn't read in the 2nd, 3rd, or 4th. What am I doing wrong?? Thank you!
string line;
while (getline (datafile, line)) // read one record upto \n
{ stringstream ss(line);
getline (ss, policy, ','); // get first field up to delim
getlne (ss, gender, ','); // get second field up to delim
// etc for remaining fields.
// numeric fields will require an extra level of parsing
string temp4;
getlne (ss, temp4, ','); // get numeric field upto delim
stringstream field4 (temp4);
field4 >> Age;
}