0

I have commented the problem into the code below. See the Store2 vector in the Write Data function. I get error 0xC0000005: Access Violation with no warnings. Any help figuring out why would be appreciated.

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

struct Weather
{
    int winddr;
    double windsp;
    double ETTsea;
	double Hsea;
    double VMDsea;
    double ETTswell;
	double Hswell;
	double VMDswell;
	double Hs;
};

struct Found {
    double height;
	double degrees;
	double prob;
	double count;
};

struct Found2 {
    double height;
	double VMDsea;
	double VMDswell;
	int yes;
};

//-----------------------------------------------------------------------------------------------------------------------------
void Write_Data(vector <Weather> Data) {
  
  double count, y, z, angle1, angle2, count2;  
  int x, a, b;
  Found Temp;
  Found2 Temp2;
  vector <Found> Store;
  vector <Found2> Store2;
  
  count2 =0;
  
    for (b=0; a<Data.size(); a++) {
      Temp2.height =0;
	  Temp2.VMDsea =0;
	  Temp2.VMDswell =0;
	  Temp2.yes=0;
	  Store2.push_back(Temp2);
	}//for statement
  
  
  for (y=0; y<=20.0; y = y+.5) {
	 if (angle1=0 && Data[x].Hs <= y)
	    count2 = count2+1;
		  
	Temp.count = count2;
	Temp.degrees = 0;
	Temp.height = y;
	Temp.prob = count2/105192.0;
	count2 = 0;
	Store.push_back(Temp);
	  for (z=15; z<=180.0; z = z + 15.0) {
		  count =0;
		  for (x=0; x<Data.size(); x++) {	  
			angle1 = abs(Data[x].VMDsea - Data[x].VMDswell);
			angle2 = abs(360-angle1);
			if (angle1>180.0) {
				angle1=angle2;
			} //for if
			if (angle1> z-15 && angle1 <=z && Data[x].Hs <= y) {
	     		count = count +1;
                if (y==20) {
			Temp2.height =Data[x].Hs;
			Temp2.VMDsea =Data[x].VMDsea;
		                Temp2.VMDswell =Data[x].VMDswell;
			Temp2.yes=1;
     //THIS IS WHERE PROGRAM ERRORS WITH 0xC0000005: Access Violation
                                                Store2[x] = Temp2;
				} //for if
			} //for if
		  }  //for x
		    Temp.count = count;
			Temp.degrees = z;
			Temp.height = y;
			Temp.prob = count/105192.0;
			Store.push_back(Temp);
			count=0;
		}  //for z
  cout<<y<<endl;
  }  //for y


  ofstream DataFile("results.txt");
  cout<<"Writing...Please wait...."<<endl;
  for (a=0; a<Store.size(); a++)  {
     DataFile << Store[a].height <<" "<< Store[a].degrees <<" "<< Store[a].prob <<" "<<Store[a].count<<endl; 
	 cout<<a/Store.size()<<endl;
  }
  ofstream DataFile2("results2.txt");
  cout<<"Writing...Please wait...."<<endl;
  for (b=0; b<Store.size(); b++)  {
     DataFile2 << Store2[b].height <<" "<< Store2[b].VMDsea <<" "<< Store2[b].VMDswell <<" "<<Store2[b].yes<<endl; 
  }
 
}  //for function

//-----------------------------------------------------------------------------------------------------------------------------
vector <Weather> Load_Data() {
  string DataStream1;
  vector<Weather> Data;

  cout<<"Pulling in data...Please wait...."<<endl;


 //Load First Data File
  ifstream DataFile1("GROW.txt");
  while(getline(DataFile1, DataStream1))
  {
	  istringstream Incoming(DataStream1);
      Weather w;
      Incoming >> w.winddr >>w.windsp >>w.ETTsea >> w.Hsea >> w.VMDsea >> w.ETTswell >> w.Hswell>> w.VMDswell >> w.Hs;
      Data.push_back(w);
  }

  //Load Successful
  cout<<"Load successful..."<<endl;
  cout<<"Array size "<<Data.size()<<endl;
  return Data;
}

//-----------------------------------------------------------------------------------------------------------------------------
int main()
{
  //Declare variables
  vector<Weather> Data;
  //Call the functions
    Data = Load_Data();
	Write_Data(Data);
return 0;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by mitrmkar
0

The value of 'x' is invalid subscript at the point where the error occurs, it exceeds the size of Store2.

You are using uninitialized variables (x, a) there, it's a bad practice.

void Write_Data(vector <Weather> Data) {
  
  double count, y, z, angle1, angle2, count2;  
  int x, a, b; 
[B]  // what might be the value of 'a' at this point?[/B]
  cout << "a is initially: " << a << '\n';

  Found Temp;
  Found2 Temp2;
  vector <Found> Store;
  vector <Found2> Store2;
  
  count2 =0;
  
    for (b=0; a<Data.size(); a++) {
      Temp2.height =0;
	  Temp2.VMDsea =0;
	  Temp2.VMDswell =0;
	  Temp2.yes=0;
	  Store2.push_back(Temp2);
	}//for statement

 [B]   // how many entries were added to Store2 ...[/B]
    cout << "Store2 size: " << Store2.size() << '\n';
...

Then you have ...

if (angle1=0 && Data[x].Hs <= y)
	    count2 = count2+1;

Did you mean comparison instead?

if (angle1==0 && Data[x].Hs <= y)
	    count2 = count2+1;

Note that in Data[x].Hs above, x is also uninitialized but doesn't trigger any error due to the malformed if condition ...

So revise your code and check how it behaves after that ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.