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
|
#include<iostream> //for cout and cin
#include<conio.h> //for getch()
#include<cmath>
#include<fstream> //to intract with files
#include<string>
#include<cstring>
#include <ctime> // for time(0)
#include <stdlib.h> /* atof */
#include <vector>
#include <string.h>
using namespace std;
string parseCsvRow ( string row, int commaOffset ) ;
int main(){
string accountName;
int r=0;
int numberOfTransactions = 0;
string date,
type,
description;
float amount;
float balace;
cout << "what is your account name?" << endl;
cin >> accountName;
ifstream accountFile;
accountName+= ".csv"; //add to the account name that user put to .csv.
accountFile.open(accountName.c_str());
while (accountFile.good()){
string row;
getline (accountFile, row);
date[r] = parseCsvRow(row, 0);
type[r] = parseCsvRow(row,1);
description[r] = parseCsvRow(row, 2);
amount[r] = atof(parseCsvRow(row,3).c_str());
numberOfTransactions++;
r++;
}
accountFile.close();
}
string parseCsvRow ( string row, int commaOffset ) {
int commaIndex = row.find(",",0);
if ( commaIndex == -1 )
return "";
commaOffset++;
row += ",";
commaIndex = 0;
int lastCommaIndex = 0;
string data;
for(int i = 0; i < commaOffset; i++ ) {
lastCommaIndex = commaIndex;
if(lastCommaIndex > 0) {
lastCommaIndex++;
}
commaIndex = row.find(",",lastCommaIndex);
if(commaIndex == -1)
return "";
data = row.substr(lastCommaIndex, commaIndex - lastCommaIndex);
}
return data;
}
|