Strings

I need help separating a file into strings. The data looks like this:

abe lincoln, 35.2, 10.75
george bush, 40, 20.50
b clinton, 50, 20
john adams, 45, 12.00

The instructor wants us to separate each item into its own string and convert the strings for hours worked and hrly wage into doubles. My primary question is how to separate the strings using the function he provided to us.

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
#include <string>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int split(string s, string ch, string temp[]) {  
  //splits a string into an array of strings using ch as delimiter
  int cnt=0, ndx;
  while (s!="") {   
    ndx=s.find(ch);
    if (ndx<0) {temp[cnt]=s; s=""; }
    else if (ndx==0) {temp[cnt]=""; s=s.substr(1); }
    else {temp[cnt]=s.substr(0,ndx);s=s.substr(ndx+1);  }
    cnt++;
  }
  return cnt;
}


int main() {
	cout << setw(12) << right << "emp name" << setw(12) << right << "gross pay" << setw(12) << right << "tax" << setw(12) << right << "net pay" << endl;
	ifstream inf;
	string z, cnt;
	double i;
	string s1;
	inf.open("c:\\temp\\program5_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program5_data.txt";
       system("pause");
       exit(0);    
	}
	//extract data from file
	getline(inf, s1, '\n');
	split(s1," ", );
	
	//take extracted data and find tax, net, and gross pay
	double hours, hrly, gross, tax, net;
	hours=0;
	hrly=0;
	tax=0;
	net=0;
	if (hours<=40){
		gross=hours*hrly;
	}
	else{ gross=(((hours-40)*hrly*1.5)+(40*hrly));
	}
	if (gross>1000){
		tax=gross*.2;
	}
	else{tax=gross*.1;
	}

	net=gross-tax;
	
	system("pause");
}


an aside: your instructor's mixing of C and C++ paradigms for that third argument string temp[] is rather bizarre - it would be much better as vector<string>& results, but maybe you haven't learned STL or references yet - in its present form, I need to look inside the implementation of his/her split() to understand what to pass into the function

split() requires that you pass in an array of strings at least big enough to hold the number of items to be split into - in your case, that number is 3

so, if you define an automatic variable string items[3]; and pass items into the third argument of split() on Line 36, you should be set (I'm assuming the rest of the code works - I haven't compiled or tried it out)
Last edited on
Thanks for the help but now I am getting

Unhandled exception at 0x00297006 in Assignment 5.exe: 0xC0000005: Access violation writing location 0xcccccccc.

1
2
3
4
//extract data from file
	getline(inf, s, '\n');
	split(s,"", arr);
	cout << arr[3] << endl;
hmm... ...what did you put in the second argument for split() - a null string?

don't you want to split on a comma?

","

also, in C/C++, arrays are zero-based - that means for string arr[3];, you have arr[0], arr[1], and arr[2], but no arr[3]
Topic archived. No new replies allowed.