-1

I feel like I'm completely missing out on something but my compiler shows absolutely nothing when i test out if my array is getting filled with values from the txt file.

void orderID(){
  ifstream result;

  int flag;
  int loop = 0;
  string temp;

  string line;
  string myArray[flag];

  result.open("resultat.txt");
  while(getline(result, line)){
    flag++; //number of lines in file
  }
  result.close();

  result.open("resultat.txt");
  while(getline(result, line)){
    myArray[loop] = line;
    loop++;
    cout<< myArray[1];
  }
  result.close();
}

Should probably post the txt file im testing. It should work with any file though.

21 para 21 first
23 dyta 23 second
11 katert 11 fourth
12 pest 12 fifth
13 fundit 13 last
14 jojo 14 nono

Anyone knows what I'm missing here? Btw please do not suggest vectors as I can not use them for this scenario. And yes I am calling the function from int main.

3 Answers3

1

There is a bit of a misunderstanding here:

int flag; // uninitialised, this value could be anything
...
string myArray[flag]; // 1. you haven't set flag, how can we know how big this is
                      // 2. VLA are non-standard c++, you can't do this so easily

Since you have said you cannot use a std::vector which is a bit of a bummer, you will need to handle the memory yourself. That means using new[] and delete[]. Something like this:

int flag = 0
// Use your loop to find out the size of the vector
string* myArray = new string[flag]
// use myArray the same way (aka, get things with myArray[myIndex])
// make sure you check for out of bounds operations
delete[] myArray // This is super important, when you use new, you must use delete. 

This is the basic approach, but to really wow, your teacher, just write a minimal vector! What do we need for this?

  1. Something that we construct that only has the correct size possible.
  2. A way to access the elements.
  3. Correct destruction.

So something like this would suffice:

class MyVector {
  public:
    MyVector(unsigned size) { // Constructor for a specific size
        array_ = new string[size];
    }

    string& operator[](unsigned index) { // A way to access the elements
        return array_[index];
    }

    ~MyVector() { // A way to destroy it
        delete[] array_;
    }

    // You should really delete copy constructor and others (rule of 5) but that
    // is a little advanced for this. 

  private:
    string* array_;

};

Then you can use it in your code quite nicely:

int flag = 0
// ...
MyVector myArray(flag);
// ...
    myArray[someIndex] = someThing;
//...
//... No need to delete, that is handled by the class.       

It is safe (well, safer), a little bit re-usable, and well encapsulated.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0

You need to initialize myArray with the correct size, which means after you compute the flag not while it is undefined

void orderID(){
  ifstream result;

  int flag = 0;
  int loop = 0;
  string temp;

  string line;

  result.open("resultat.txt");
  while(getline(result, line)){
    flag++; //number of lines in file
  }
  result.close();

  string myArray[flag];

  result.open("resultat.txt");
  while(getline(result, line)){
    myArray[loop] = line;
    loop++;
    cout<< myArray[1];
  }
  result.close();
}
moustafa
  • 251
  • 2
  • 11
  • It's true what you said. I had to introduce the array a bit later. Thank you very much for the solution – rrexha.Tirana Jun 08 '21 at 11:34
  • This is not according to the C++ standard ([https://stackoverflow.com/questions/17318007/c-variable-length-array](C++ VLA)). For dynamic arrays, when std::vector can't be used for whatever reason, new[]/delete[] should be used. It is supported by several compilers, so if it works for you, that's fine. Beware that larger arrays can cause stack-overflow. – stefaanv Jun 08 '21 at 11:49
  • Thanks for your input but I literally do not understand half the things you have mentioned xd. I'm sure I will get to learn them in the near future. – rrexha.Tirana Jun 08 '21 at 11:54
0

It looks like you're trying to initialize myArray with a variable as the array length, which is not allowed in C++.

Since you can't use vectors in this code, you'll need to allocate space to the array using the new operator.

So first, you'll need to initialize flag to 0. Then, once you've counted the number of lines in the file, create myArray likewise:

string *myArray = new string[flag];

What this line does, is that it allocates memory on the heap for myArray with the size of flags.

So your code should look something like this:

flag = 0;
while(getline(result, line)){
    flag++;
}
string *myArray = new string[flag];
//...
delete[] myArray;

Once you're done with the array, you can de-allocate your array using the delete[] operator.

Nadav
  • 1