Access Violation Writing Array Reading .txt

Dear all,

I'm a newbie in any programming, and recently must work on c++. I have a difficulty in forming an array and reading a file at the same time. I have tried the code, but I couldn't understand why it is not working (access violation error). Appreciating any advise. Thank you..

[input .txt]
Knt
1 1 0 1000 1000 1000
[input .txt]

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

#include <fstream>
#include <iostream>

using namespace std;

class sce{
 public:
 double getK(int nN, int nT) { return K[nN][nT]; }
	void setK(int nN, int nT, double newK) { K[nN][nT] = newK; }

 void initK(int nC, int nN, int nT){
   //...another function with int nC
   K = new double*[nN];
   for (int n = 0; n < nN; n++) {
	K[n] = new double[nT];
		}
 }
 ~sce(){};
 private:
 double** K;
};

int main(){
 int n, a, b;
 int temp;
 double tempDouble;
 char tempChar;
 ifstream file; 	
 string line;
 string filename = "data\\data" + to_string(dset) + ".txt";
	
 file.open(filename.c_str());
 if (file.is_open()) {
	cout << "Can open file!" <<endl;}
 else { cout << "Can not open file!" << endl;
	exit(1);}
 
 std::getline(file, line);
 file >> temp; gen.setnN(temp); //calling the gen class
 file >> temp; gen.setnC(temp);
 file >> temp; gen.setnT(temp);
 file >> temp; gen.setnS(temp);
 sce = new SCE[gen.getnS()];

 for (s = 0; s < nS; s++) {
      sce[s].initsce(nC, nN, nT);}
 int ni = nN * nS;
 
  //get K
 file >> tempChar; getline(file, line);
 for (int i = 0; i < ni; i++) {
    file >> a;
    file >> b; 
    for (t = 0; t < nT; t++) {
        file >> tempDouble; 
        sce[a].setK(b, t, tempDouble);	
    }
 }

 return();
};
12:17: warning: unused parameter 'nC' [-Wunused-parameter]
In function 'int main()':
31:45: error: 'dset' was not declared in this scope
40:16: error: 'gen' was not declared in this scope
44:6: error: expected unqualified-id before '=' token
46:7: error: 's' was not declared in this scope
46:18: error: 'nS' was not declared in this scope
47:10: error: expected unqualified-id before '[' token
48:11: error: 'nN' was not declared in this scope
48:16: error: 'nS' was not declared in this scope
55:10: error: 't' was not declared in this scope
55:21: error: 'nT' was not declared in this scope
57:12: error: expected unqualified-id before '[' token
61:9: error: expected primary-expression before ')' token
25:6: warning: unused variable 'n' [-Wunused-variable]
At global scope:
62:2: warning: extra ';' [-Wpedantic]


Best advice is to use a std::vector and not arrays, and don't do manual memory management. Try to avoid using pointers.

Try to write that is easy to read, choose good variable and function names.

Always initialise your variables. Read the manual.

Must have an object of the class type, before one can call the class functions
check the obvious pointer problems or get rid of the pointers.
.. is the outer dimension correct in your debugger?
the inner dimension?
the loop iterations, are they also the expected bounds?

is there a reason for the pointers here? Would a vector work?
line 48 looks funky to me.
you say ni is nn*ns but you allocated (init function) with nn
its a little hard to follow the variable names....
Last edited on
Dear Jonnin and TheIdeasMan,

Thank you for the advise. I will reprocess the array with CPLEX but let me try to initiate it with vector, as well as find better way to revise line 48. Thank you
Topic archived. No new replies allowed.