C++ programming: Create a program that accepts comma seprated stream of data in
ID: 3785228 • Letter: C
Question
C++ programming: Create a program that accepts comma seprated stream of data in CSV format reads odometer and gallons from a file, calculate mile per gallon, and average mile per gallon then write it back to the file in CSV format. The following data is in (Date Odometer Gallons Cost This_MPG Average MPG) format.
//test.csv
4/23/95 ,36381.1,12.7,28.8
4/26/95 ,36545.2,4.5,9.2,36.5,36.5
4/29/95,36712.9,8.2,17.7,20.5,26.1
5/2/95,36845.7, 4.4, 3.4, 30.2, 27.2
5/7/95,37083.2,7.8,13.6,30.4,3363
5/11/95,37357.9,10.3,12.2,26.7,27.8
5/17/95,37732.6,12.4,23.5,30.2,28.4
5/21/95,37933.2,5.8,7.3,34.6,29.1
5/25/95,38158.6,8.5,6.9,26.5,28.7
5/27/95,38286.5,5.4,9.6,23.7,28.3
6/9/95,38640.9,13.7,20,25.9,27.9
6/11/95,38775.9,10.3,8.4,13.1,26.2
6/17/95,39089.1,12,24.1,26.1,26.2
6/20/95,39251,9.3,19.1,17.4,25.5
6/22/95,39365.7,5.5,9.4,20.9,25.3
6/27/95,39558.5,4.9 9,39.3,25.8
7/2/95,39801.1,11.2,22.9,21.7,25.5
7/8/95 ,39930.6,10.6,21.5,25.3
7/11/95,40049.8,4.3,4.2,27.9,25.4
7/17/95 ,40433.2,11.3,25.9,33.9,26
7/28/95 ,40954.7,13.8,20.4,37.8,27
8/9/95,41284.1,8.7,19.2,37.9,27.5
8/16/95,41504.2,7.1,9.7,31,27.6
8/19/95,41635.5,10,20.5,13.1,26.9
8/28/95 41886.9 12 17 21 26.5
9/4/95 42097.7 9 13.9 23.4 26.4
9/5/95 42179.9 4.5 8.8 18.3 26.3
9/9/95 42417.3 13.5 19.2 17.6 25.8
9/23/95 42709.8 10.3 19.1 28.4 25.9
9/29/95 43066.8 10.7 13.7 33.4 26.2
10/2/95 43284.4 13.2 26.1 16.5 25.7
10/6/95 43446.2 10.9 9.8 14.8 25.3
10/9/95 43574.1 8.7 20.7 14.7 25
10/12/95 43743.4 5.1 11.4 33.2 25.1
10/17/95 43899.8 11.5 22.7 13.6 24.7
10/19/95 44015.7 6 8.2 19.3 24.6
10/27/95 44328.4 13 30.2 24.1 24.5
10/29/95 44528.6 11.2 18.4 17.9 24.3
11/2/95 44748.5 8.3 15.6 26.5 24.4
11/8/95 44967.4 5.9 13.8 37.1 24.6
11/11/95 45105.6 4.4 9.8 31.4 24.7
11/18/95 45528.9 11.1 19.2 38.1 25.1
11/23/95 45798.4 8 8.1 33.7 25.3
12/1/95 46034.1 9.9 10.4 23.8 25.2
12/7/95 46255.9 7.3 10.7 30.4 25.3
12/13/95 46402.8 6.4 6.5 23 25.3
12/26/95 46798.8 13.8 28.2 28.7 25.4
12/31/95 47047.1 9.6 9.2 25.9 25.4
1/4/96 47241.2 7.9 16.6 24.6 25.4
1/10/96 47569.8 12.6 26.8 26.1 25.4
Explanation / Answer
#include <fstream.h>
#include <iostream.h>
#include <sstream.h>
#include <string.h>
#include <vector.h>
using namespace std;
typedef vector <double> record_t;
typedef vector <record_t> data_t;
typedef vector <double> odometer;
typedef vector <double> gallons;
typedef vector <double> mpg;
istream& operator >> ( istream& input, record_t& record )
{
record.clear();
string line;
getline( input, line );
stringstream ss( line );
string field;
while (getline( ss, field, ',' ))
{
stringstream fs( field );
double f = 0.0;
fs >> f;
record.push_back( f );
}
return input;
}
istream& operator >> ( istream& input, data_t& data )
{
data.clear();
int calmpg(data);
record_t record;
while (input >> record)
{
data.push_back( record );
}
return input;
}
int calmpg(data)
{
mpg= odometer / gallons;
return mpg;
}
int main()
{
data_t data;
ifstream infile( "test.txt" );
infile >> data;
if (!infile.eof())
{
cout << "Error! ";
return 1;
}
infile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.