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
|
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct dataRecord {
string borough;
string law_category;
string offense_desciption;
string station_name;
};
bool readCSVtoArray (string, dataRecord[], int);
void printRecord(dataRecord);
void print5(dataRecord[]);
int numMisdemeanor(dataRecord[], int);
void printBrooklynData(dataRecord[], int);
int main(){
string filename = "NYPDcomplaints.csv";
const int size = 6053;
dataRecord myRecords[size];
if (!readCSVtoArray(filename, myRecords, size)){
cout << "Could not read file.";
}
else {
print5(myRecords);
cout << "\nFalse Misdemeanor: " << numMisdemeanor(myRecords, size);
cout << "\nBrooklyn: Misdemeanor:\n";
printBrooklynData(myRecords, size);
}
return 0;
}
bool readCSVtoArray (string fn, dataRecord recArray[], int size) {
bool success = false;
ifstream infile;
string line;
infile.open(fn.c_str());
if (infile) {
cout << "File opened\n";
getline(infile, line);
for (int i = 0; i < size; i++) {
getline(infile, line);
istringstream rowData(line);
getline(rowData, recArray[i].borough, ',');
getline(rowData, recArray[i].law_category, ',');
getline(rowData, recArray[i].offense_desciption, ',');
getline(rowData, recArray[i].station_name, ',');
}
success = true;
}
return success;
}
void printData(dataRecord record) {
cout << "\nBorough: " << record.borough << endl;
cout << "\nIncident Law Category: " << record.law_category << endl;
cout << "\nIncident Offense Description: " << record.offense_desciption <<endl;
cout << "\nAverage Station Name: " << record.station_name << endl;
}
void print5(dataRecord recArray[]) {
cout << "First 5 records: \n";
for (int i = 0; i < 5; i++) {
printData(recArray[i]);
}
}
int numMisdemeanor(dataRecord recArray[], int size) {
int MisdemeanorCount = 0;
for (int i = 0; i < size; i++) {
if (recArray[i].law_category == "Misdemeanor")
MisdemeanorCount += stoi(recArray[i].law_category);
}
return MisdemeanorCount;
}
void printQueensData(dataRecord recArray[], int size) {
for (int i = 0; i < size; i++) {
if (recArray[i].borough == "Brooklyn" && recArray[i].law_category == "Misdemeanor")
printData(recArray[i]);
}
}
|