reading CSV file

Hi, I have a CSV file and I want to read the CSV file in .dat format. I have done the code and it managed to show data however, it only shows one data and the data is not the same as the csv file (perhaps is my varibale declare wrongly). I am aware that I need to do using loop to show all data but I am unsure how to start. Anyone can guide me on the variable for the data and also how to use looping?

My CSV data (Stat.csv) is as such
Petrol Price Stat
Time Cost (Per Litre) No. of Litre Total Cost
10/10/2015 15:55 2.31 200 462
11/10/2015 16:00 2.21 12 26.52
12/10/2015 12:00 2.4 125 300
12/10/2015 18:00 2.85 458 1305.3

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
  #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{

ifstream inFile;
ofstream outFile;

string date;
double cost;
int no_of_litre;
double total_cost;

inFile.open("Stat.csv");
outFile.open("Stat.dat");

inFile >> date >> cost >> no_of_litre >> total_cost;
outFile << date << " " << cost << "" << no_of_litre << "" << total_cost << endl;

inFile.close();
outFile.close();

system ("PAUSE");
return 0;
}
CSV stands for 'Comma Separated Values'. But usually the delimiter is a semicolon. To read such delimited data use getline(...):

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

which takes string only:

1
2
3
4
5
// Note: getline() returns [indirectly through istream] false if end of file or another error occurs
while(getline(inFile, date, ';') && getline(inFile, cost_str, ';') && getline(inFile, no_of_litre_str, ';') && getline(inFile, total_cost))
{
outFile << date << " " << cost_str << "" << no_of_litre_str << "" << total_cost_str << endl;
}


To remove the header(s) you might add another getline() before the loop.

Note that you get only strings. If you want numeric values you might use stringstream within the loop. See

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream
Hi, I am still trying to understand from the link example but I am still rather confused. Are you able to show me how do I edit from 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
26
27
28
29
30
31
32
33
34
35
36
37

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{

ifstream inFile;
ofstream outFile;

string date;
double cost;
int no_of_litre;
double total_cost;

inFile.open("Stat.csv");
outFile.open("Stat.dat");


// Note: getline() returns [indirectly through istream] false if end of file or another error occurs
while(getline(inFile, date, ';') && getline(inFile, cost_str, ';') && getline(inFile, no_of_litre_str, ';') && getline(inFile, total_cost))
{
outFile << date << " " << cost_str << "" << no_of_litre_str << "" << total_cost_str << endl;
}
 

inFile.close();
outFile.close();

system ("PAUSE");
return 0;
}
 
What is so confusing regarding those statements? Where is the problem?
Hi, there are some changes to my code. However, currently I need some advice for the following:
1) How to remove duplicate date from the printout
2) How to enter a new line from the printout for the time.
And the print out doesnt seems to be tally

Below is my partial code for printing out the data

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

bool exit = false;
	while (exit==false) {
        cout << "Menu Option" << "\n" 
			<< "1. Highest Price and Start Time" << "\n"
			<< "2. Lowest Price and Start Time" << "\n"
			<< "3. To End" << "\n" << "\n";
        cout << "Please input a value between 1 to 3: ";
		cin >> menuOption;
		cout << endl;

		switch (menuOption) {
		case 1:
		{
			int index = 0;
			double High = 0.0;
			
			for (int j = 0; j < salesVector.size(); j++) {
				if (salesVector[j].price > High) {
					index = j;
				}
			}
				cout << "Highest Price and Start Time" << endl;
				cout << "Date: " << salesVector[index].date << endl;
				cout << "Highest Price: $" << salesVector[index].price << endl;
				cout << "Starting Time: " << salesVector[index].time << "\n" << endl;
			exit = false;
			break;
		}

		case 2:
		{
			int index2 = 0;
			double Low = 0.0;
			
			for (int j = 0; j < salesVector.size(); j++) {
				if (salesVector[j].price < Low) {
					index2 = j;
				}
			}
			cout << "Lowest Price and Start Time" << endl;
			cout << "Date: " << salesVector[index2].date << endl;
			cout << "Lowest Price: $" << salesVector[index2].price << endl;
			cout << "Starting Time: " << salesVector[index2].time << "\n" << endl;
			exit = false;
			break;
		}

		case 3:
			exit = true;
			break;
		default:
			cout << "Error, Please Enter Again." << "\n" << endl;
			exit = false;
			break;
		}
	
	}
}


I am unable to upload any attachement, however, below is my file data

Petrol Statistics
Date/Time,Price ($),Volume,Value ($)
10/10/2013 4:57,5.81,5000,29050
10/10/2013 7:48,5.81,62728,364449.68
10/10/2013 16:10,5.82,100,582
10/10/2013 17:10,5.86,150,879
10/10/2013 17:11,5.23,169,883.87
10/10/2013 17:14,5.81,451,2620.31
10/10/2013 18:10,5.81,5000,29050


Below is my output results
Highest Price and Start Time
Date: 10/10/2013 10/10/2013
Highest Price: $5.82
STarting Time: 4.57:48 18:10:48

Lowest Price and Start Time
Date: 10/10/2013
Highest Price: $5.82
STarting Time: 4.57:48
Topic archived. No new replies allowed.