Thanks for your help earlier with the file input everyone...I have another question to ask of you. I am trying to check two vectors against each other to see if the "timeanddate" match up. Then try to output the ones that do match. I am getting errors from the if statement down, as the inputting works flawlessly. Any help would be great! Thanks in advance.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std ;

struct Weather
{
    string timeanddate;
    double wind;
	double windmax;
};

struct Weather2 {
	string timeanddate;
    string excel;
	string date;
	string time;
	int windr;
	double wind;
	double windmps;
};

struct Golf {
	string timeanddate;
	double wind_obs;
	double wind_sensor;
};

int main()
{


  int a =0, i,j;
  
  string DataStream1;
  string DataStream2;
  string DataStream3;

  vector<Weather> Sensor;
  vector<Weather2> Observations;
  vector <Golf> Search;
  
  Golf Foundb;

  ifstream DataFile1("Wind Sensor Data.txt");
  while(getline(DataFile1, DataStream1))
  {
      istringstream Incoming(DataStream1);
      Weather w;
      Incoming >> w.timeanddate >> w.wind >> w.windmax;
      Sensor.push_back(w);
  }
  
  ifstream DataFile2("Wind Observation.txt");
  while(getline(DataFile2, DataStream2))
  {
      istringstream Incoming(DataStream2);
      Weather2 x;
      Incoming >> x.timeanddate >> x.excel >> x.date >> x.time >> x.windr >> x.wind  >> x.windmps;
      Observations.push_back(x);
  }
\\HERE BEGINS THE PROBLEM...

  for(i= 0; i <= Sensor.size(); i++) {
	  for (j=0; j <=Observations.size(); j++) {
		  if (Sensor[i].timeanddate==Observations[j].timeanddate) {
			 Foundb.timeanddate = Sensor[i].timeanddate;
			 Foundb.wind_obs = Observations[j].wind;
			 Foundb.wind_sensor = Sensor[i].wind;
			 Search.push_back(Foundb);
	      }
	  } 				
  }

  ofstream DataFile3("C:\Documents and Settings\ttrusse\Desktop\Algeciras\Matchup.txt");
  for (a=0; a<=Search.size(); a++)  {
     ostringstream Outgoing(DataStream3);
     Outgoing << Search[a].timeanddate << Search[a].wind_obs <<Search[a].wind_sensor<<endl;
  }

return 0;
  }

Debug Window Shows...

Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
First-chance exception in sensor.exe: 0xC0000005: Access Violation.

Recommended Answers

All 6 Replies

Use the < operator instead of <= in both of your for loops, i.e.

for(i= 0; i < Sensor.size(); i++) {
	  for (j=0; j < Observations.size(); j++) {
  }

Thanks!

Still have a problem. The data file has no data in it. Code is attached below. Any recommendation with this?

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std ;

struct Weather
{
    string date;
	string time;
    double wind;
};

struct Weather2 {
	string date;
	string time;
	double wind_obs;
	double wind_sensor;
};

int main()
{


  int a =0, i=0,j=0;
  
  string DataStream1;
  string DataStream2;
  string DataStream3;

  vector<Weather> Sensor;
  vector<Weather> Observations;
  vector <Weather2> Search;
  
  Weather2 Found;

  cout<<"Pulling in data...Please wait...."<<endl;
  ifstream DataFile1("Wind Sensor Data.txt");
  while(getline(DataFile1, DataStream1))
  {
      istringstream Incoming(DataStream1);
      Weather w;
      Incoming >> w.date >> w.time  >> w.wind;
      Sensor.push_back(w);
  }
  
  ifstream DataFile2("Wind Observation.txt");
  while(getline(DataFile2, DataStream2))
  {
      istringstream Incoming(DataStream2);
      Weather x;
      Incoming >> x.date >> x.time >> x.wind;
	  Observations.push_back(x);
  }
  cout<<"Checking data...Please wait...."<<endl;
  for(i= 0; i < Sensor.size(); i++) {
	  for (j=0; j <Observations.size(); j++) {
		  if ((Sensor[i].date==Observations[j].date) && (Sensor[i].time==Observations[j].time)) {
			 Found.date = Sensor[i].date;
			 Found.time = Sensor[i].time;
			 Found.wind_obs = Observations[j].wind;
			 Found.wind_sensor = Sensor[i].wind;
			 Search.push_back(Found);
	      }
		  
	  } 				
  }

  ofstream DataFile3("C:\Documents and Settings\ttrusse\Desktop\Algeciras\C++\results.txt");
  cout<<"Writing...Please wait...."<<endl;
  cin.get();
  for (a=0; a<Search.size(); a++)  {
	 ostringstream Outgoing(DataStream3);
     Outgoing << Search[a].date << Search[a].time << Search[a].wind_obs <<Search[a].wind_sensor<<endl;  
  };

 return 0;
  }

You have to escape all backslashes in the file path, i.e.
instead of

ofstream DataFile3("C:\Documents and Settings\ttrusse\Desktop\Algeciras\C++\results.txt");

use

ofstream DataFile3("C:\\Documents and Settings\\ttrusse\\Desktop\\Algeciras\\C++\\results.txt");

Didn't your compiler give any warnings??

Just that it didn't recongnize 'C' or 'D' etc in the file path...For some reason though, there is still no output in the results.txt. The cout was added to make sure that the Search vector was filled up (it was)...now if it would just output, it'd be all done...any help again? Thanks in advance.

ofstream DataFile3("C:\\Documents and Settings\\ttrusse\\Desktop\\Algeciras\\C++\\results.txt");
  cout<<"Writing...Please wait...."<<endl;
  for (a=0; a<Search.size(); a++)  {
	 cout<<Search[a].date<<" "<<Search[a].time<<" "<<Search[a].wind_obs<<" "<<Search[a].wind_sensor<<endl;
	 ostringstream Outgoing(DataStream3);
     Outgoing << Search[a].date << Search[a].time << Search[a].wind_obs <<Search[a].wind_sensor<<endl;

This is probably what you are after ...

<snip>
ofstream DataFile3("C:\\Documents and Settings\\ttrusse\\Desktop\\Algeciras\\C++\\results.txt");

cout<<"Writing...Please wait...."<<endl;
cin.get();

for (a=0; a<Search.size(); a++) 
{
    // [B]No ostringstream is needed here[/B], 
    // instead output directly to the file ...
    DataFile3 
               << Search[a].date 
               << Search[a].time
               << Search[a].wind_obs 
               << Search[a].wind_sensor
               << endl;  
}
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.