Reading in space separated versus comma separated files

Hello everyone! I just wrote a code that reads in a text file called "policies.txt" 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


This works perfectly fine, but what if I want each element of the table to be separated by commas. For example:
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


or better yet, what if I want it to not matter whether the columns are separated by commas or spaces? is there any way to do this? If there is no way to read in both comma-separated and space-separated elements simultaneously then I would prefer just comma, rather than the space separated which my code is able to read now. What modifications would I have to make to my code to make this work?
This is my code to reference. Thanks!
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
103
104
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>
#include <stdio.h>
using namespace std;
#define STARTAGE 15
int main(int argc, char ** argv) {
	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.txt");
		a_file.precision(2);
		a_file.setf(ios::fixed);
		a_file.setf(ios::showpoint);
		while (!datafile.eof()){
			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";
		}
	}
	printf(" Press enter to exit\n");
	getchar();
	return 0;
}
try using the [getline] function on your [datafile] file stream to read an entire line out of the input file at a time.

once a line is read we should check if it has a comma in it - if so we should parse the line with comma logic otherwise we should parse it with space logic.
Checkout string::getline.
http://www.cplusplus.com/reference/string/string/getline/

The second form will read up a specified delimiter.
Usually used in conjunction with stringstream to parse out integers, floats and doubles from the string just read.

1
2
3
4
  string field;
  getline (datafile, field, ',');
  stringstream ss(field);
  ss >> age;
I made these adjustments to my code:
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
int main(int argc, char ** argv) {
	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 field;
	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.txt");
		a_file.precision(2);
		a_file.setf(ios::fixed);
		a_file.setf(ios::showpoint);
		while (!datafile.eof()){
			getline(datafile, field, ',');
			stringstream ss(field);
			ss >> policy >> gender >> smoker_status >> age >> death_ben >> db_option >> interest >> end_age;

(the rest is the same as above)
however now my output is only printing out my first policy. It looks like this:
Premium for Pol1 is $14101.62
when it should look like this:
1
2
3
4
Premium for Pol1 is $14101.62
Premium for Pol2 is $14221.16
Premium for Pol3 is $582390.50
Premium for Pol4 is $220384.49

how come the while loop is stopping after the first policy? I want it to read all four
each field element retrieved by the getline statement on line 23 will retrieve each individual field, ie on first call it should return the policy, on second the gender, ...

your stringstream logic on line 24 where you pass through this field then doesn't seem correct.

maybe you should be doing:

1
2
3
4
5
6
7
8
getline(datafile, policy, ',');
getline(datafile, gender, ',');
getline(datafile, smoker_status, ',');
getline(datafile, age, ',');
getline(datafile, death_ben, ',');
getline(datafile, db_option, ',');
getline(datafile, interest, ',');
getline(datafile, end_age, ',');
Topic archived. No new replies allowed.