- #1
davidsmith315
- 3
- 0
Hello, I'm trying to make part of a code that reads a csv file with data containing the date and time, and the wind speed at that time, then does various output things.
When I copy and paste 1000 entriesinto a csv file and ru it, it works great, but if I use the file that's about 6,000kb it misses every second date and time part out, code anyone help me with this?
I'm a complete novice to programming- this is part of my Electrical Engineering course.
heres my code:
When I copy and paste 1000 entriesinto a csv file and ru it, it works great, but if I use the file that's about 6,000kb it misses every second date and time part out, code anyone help me with this?
I'm a complete novice to programming- this is part of my Electrical Engineering course.
heres my code:
Code:
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include <FileHC.h>
using namespace std;
int main ()
{ ofstream outFile("C:\\WindSpeedAnswer.csv");
if (!outFile) {
cout<< "error opening file"<<endl;
return -1;
}
ifstream inFile ("C:\\Copy of Kirkwall_23_hm_proc15.csv");
string line;
int linenum = 0;
if (inFile.is_open())
{
double randomMax;
double randomMin;
cout<<"would you like to add a random varience, enter 'yes' if you do "<<endl;
string Question;
cin >> Question;
string answerYes= "yes";
if (Question==answerYes){
cout<<"enter the max value of random varience "<<endl;
cin>>randomMax;
randomMax=randomMax-1;
cout<<"enter the min value of random varience "<<endl;
cin>>randomMin;
randomMin=randomMin-1;}
while (getline (inFile, line))
{
linenum++;
// cout << "\nLine #" << linenum << ":" << endl;
istringstream linestream(line);
string item;
int itemnum = 0;
while (getline (linestream, item, ','))
{
itemnum++;
double WindSpeed=0;
string DateAndTime;
//cout << "Item #" << itemnum << ": " << item << endl;
if (itemnum%2 == 1) {
DateAndTime=item;
string replace= "-99999"; while(DateAndTime==replace){
DateAndTime= "Data Missing";}
cout<<"at "<<DateAndTime<<endl;
outFile<< DateAndTime<<"," ;}
if (itemnum%2 == 0) {
WindSpeed=atof(item.c_str());
if(WindSpeed==-99999){
WindSpeed=9;
}
if (Question==answerYes){
for(int index=0; index<itemnum; index++){
double X = randomMax+ rand() * (randomMax - randomMin) / RAND_MAX;
if (randomMin<X<randomMax){
WindSpeed=X;}
}}
cout<<"the wind speed is "<<WindSpeed<<","<<endl;
outFile<<WindSpeed<<","<<endl;
//outFile.write(WindSpeed);
}
}
}
inFile.close();
}
else cout << "Unable to open file";
outFile.close();
char stopchar;
cin>>stopchar;
return 0;
}
Last edited by a moderator: