Hello, am new to this so please bear with me. I'm working on a windfarm simulator in C++, it has to read in data from a .csv file and calculate various power values. Anyway that's beside the point. The problem I'm having with is the 2 Dimensional Dynamic Array that I created to store the values as the size of each windfarm file varies..
The program runs through the .csv file once to count how big to make the array then goes through it again taking each value in tern and stores in in the array it just created.
From what I can gather it is creating the array and also storing the values but when I try to output the array it doesn't seem to work. I get the following error -
Unhandled exception at 0x60fd942c in Windfarm.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.
I think it hs something to do with trying to read memory from outside the array but I have no idea why it would be doing this. Here is my code
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
using namespace std;
int main (){
string windspeeddate, filepath, temp, **windarray ;
int windspeed= 0,rowcounter = 0, colcounter = 1 ,row,col;
cout << " Enter the name of the windspeed data .csv file \n";
cin >> filepath;
ifstream inFile;
inFile.open(filepath.c_str());
if (!inFile){
cout << " The file did not open correctly \n";
return -1;}
while (!inFile.eof()){
getline (inFile, temp , ',');
rowcounter++
}
cout << " The .csv file contains " << rowcounter <<endl;
try
{
windarray = new string *[rowcounter];
windarray[rowcounter]= new string [colcounter];
}
catch (bad_alloc& ba)
{
cerr << "bad_alloc caught: " << ba.what() << endl;
}
while (!inFile.eof()){
getline (inFile, temp , ',');
for ( row = 0; row<rowcounter;row++){for ( col = 0;col<colcounter;col++){
cout << temp << endl;
windarray[row][col] = temp;}
}
}
for ( row = 0; row<rowcounter;row++){
for ( col = 0;col<colcounter;col++)
{cout <<windarray[row][col]<< " ";
}
cout<< endl;
}
system("PAUSE");
}
Have been looking around for a solution but can't seem to figure it out. Any help from you guys would be really appreciated. If you need me to add anything to this please just ask. I've attached below the the .csv file in a .txt file format.
Thank You
Kungu