How to read in comma separated table.txt

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:
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

with absolutely no spaces inbetween the columns, only commas. What modifications would I have to make to my code in order to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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>
using namespace 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];
	double const *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!
Change that to:
1
2
3
4
5
6
7
8
9
10
11
12
    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;
    }    
Last edited on
Topic archived. No new replies allowed.