Excel file into structure
Apr 15, 2016 at 7:25pm UTC
When the stringstream copy's the data in the structures it copy's everything fine until the last column. It will not convert the blank tab in the last column to a 0. I do not know why because it converts blank tabs for the previous ones.
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <iomanip>
#include<sstream>
#include <cmath>
using namespace std;
struct Data_Weather
{
int EVENT_ID;
string CZ_NAME;
string DATE;
int TIME;
string EVENT_TYPE;
int DEATHS;
int INJURIES;
int DAMAGE;
double LAT;
double LONG;
};
void fourth_function(vector<Data_Weather> &data_holder)
{
ifstream myfile;
myfile.open("weatherdata.csv" );
if (myfile)
{
for (int x = 0 ; x < data_holder.size(); x++)
{
if (data_holder[x].LAT == 0)
{
data_holder[x].LAT == 0;
}
if (data_holder[x].EVENT_TYPE == "Heat" )
{
cout << data_holder[x].LAT << " " << data_holder[x].LONG << " " << data_holder[x].EVENT_TYPE << endl;
}
}
}
}
void injuries(vector<Data_Weather> &data_holder )
{
Data_Weather temp;
ifstream myfile;
myfile.open("weatherdata.csv" );
data_holder[0].INJURIES = 0;
if (myfile)
{
for (int i = 0 ; i < data_holder.size(); i++)
{
if (data_holder[i].INJURIES != 0)
{
cout << " In the event " << data_holder[i].EVENT_ID << " called " << data_holder[i].EVENT_TYPE << " there were " << data_holder[i].INJURIES << " injuries. " << endl;
}
}
}
}
void average(vector<Data_Weather> &data_holder)
{
ifstream myfile;
double holder = 0;
int total = 0;
double average = 0;
myfile.open("weatherdata.csv" );
if (myfile)
{
for (int i = 0 ; i < data_holder.size(); i++)
{
if (data_holder[i].EVENT_TYPE == "Thunderstorm Wind" )
{
total+= data_holder[i].TIME;
data_holder[i].EVENT_TYPE;
{
holder++;
average = static_cast <double >(total)/holder;
}
}
}
}
cout << "The average time for the event Thunderstorm Wind is " << round(average) << endl;
}
void heat_finder(vector<Data_Weather> &data_holder)
{
string hold;
ifstream myfile;
myfile.open("weatherdata.csv" );
if (myfile)
{
for (int i = 0 ; i < data_holder.size(); i++)
{
if (data_holder[i].EVENT_TYPE == "Heat" )
{
cout << "The event " << data_holder[i].EVENT_TYPE << " was on " << data_holder[i].DATE << endl;
}
}
}
}
void deaths ( vector<Data_Weather> &data_holder)
{
string hold;
data_holder[0].DEATHS = 0;
ifstream myfile;
myfile.open("weatherdata.csv" );
if (myfile)
{
for (int i = 0 ; i < data_holder.size() ; i++)
{
if (data_holder[i].DEATHS != 0)
cout << "The event " << data_holder[i].EVENT_TYPE << " had " << data_holder[i].DEATHS << " deaths on this date " << data_holder[i].DATE <<endl;
}
}
}
int main()
{
char comma;
char eol = '\n' ;
vector<Data_Weather> data_holder;
string hold;
Data_Weather temp;
string first_element;
ifstream myfile;
myfile.open("weatherdata.csv" );
getline(myfile,first_element);
if (myfile)
{
while (getline(myfile,hold))
{
stringstream string_stream_name(hold);
string_stream_name >> temp.EVENT_ID >> comma;
getline(string_stream_name, temp.CZ_NAME, ',' );
getline(string_stream_name, temp.DATE,',' );
string_stream_name >> temp.TIME >> comma;
getline(string_stream_name, temp.EVENT_TYPE,',' );
string_stream_name >> temp.DEATHS >> comma;
string_stream_name >> temp.INJURIES >> comma;
string_stream_name >> temp.DAMAGE >> comma;
string_stream_name >> temp.LAT >> comma;
string_stream_name >> temp.LONG >> eol;
{
temp.LAT == 0;
}
//cout << temp.LONG << endl;
//cout << temp.LAT << " " << temp.LONG << endl;
data_holder.push_back(temp);
}
}
/* cout << "First Function" << endl;
injuries(data_holder);
cout <<"\n";
cout << "Second Function" << endl;
deaths(data_holder);
cout <<"\n";
cout << "Third Function" << endl;
average(data_holder);
cout <<"\n";
cout << "Fourth Function" << endl;
heat_finder(data_holder);
cout <<"\n";
cout << "I will show the latitude and longitude" << endl;
cout << "Latitude " << " Longitude" << " Name" << endl;
fourth_function(data_holder);*/
Topic archived. No new replies allowed.