Can't find the solution in my program

Can anyone pinpoint the problem in my code here, i found the root of the problem in the void Rodymas() if statement, but i'm not sure why is it not processing the request.

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
  #include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;
int j;

class Tekstas
{
  protected:
  	string masyvas[];
  	string pirmas_zodis;
  	int ilgis;
  	string letter;
  public:
    Tekstas()
    {
 ifstream file ("duota.txt");
    ilgis=0;  
    if(file.is_open()){
        string word;
        while (file >> word)
        {
            ilgis=ilgis+1;
        }
        //cout<<ilgis;
    }
    file.close();
   file.open("duota.txt");
    if(file.is_open()){
        string masyvas[ilgis];
        for (int i=0;i<ilgis;i++){
            file >> masyvas[i];
            //cout<<masyvas[i];
        }
        pirmas_zodis=masyvas[0];
        sort(masyvas,masyvas+ilgis);
    }
}
        void Rodymas(){
        for(int j=ilgis;j>-1;j--){
        //cout<<"blabla";
        letter=pirmas_zodis.substr(0,1);
        //cout<<"sthsth";
            if(letter==masyvas[j].substr(0,1)){ // <---------- the problem
                cout<<masyvas[j]<<'\n'; 
                
				}
            }
        }
    

};
int main() {
Tekstas TT;
TT.Rodymas();
return 0;
}
Well your code won't even compile on my system. Here are the error messages:

main.cpp|58|error: flexible array member ‘Tekstas::masyvas’ not at end of ‘class Tekstas’|
main.cpp|55|note: in the definition of ‘class Tekstas’|
main.cpp||In constructor ‘Tekstas::Tekstas()’:|
main.cpp|64|error: unknown array size in delete|
main.cpp|85|error: ISO C++ forbids variable length array ‘masyvas’ [-Wvla]|
main.cpp||In destructor ‘Tekstas::~Tekstas()’:|
main.cpp|55|error: unknown array size in delete|
main.cpp||In function ‘int main()’:|
main.cpp|117|note: synthesized method ‘Tekstas::~Tekstas()’ first required here |


You may want to consider using std::vector instead of the arrays.

Put this before line 46 and see what you think of the outcome:

cout << "letter: " << letter " masyvas[j].substr(0,1): " << masyvas[j].substr(0,1) << '\n';
Found the problem, thanks for the help :)
Topic archived. No new replies allowed.