Problem calling values from Dynamic Array

Hi,

Here's a small piece of code I made for a dynamic array. I dont know if I am taking the best approach, but looks like its work in a rather simple way. Digs in a .txt file, and then input the values on screen. However when I quit the "While" loop, I cant find a way to display again values from that array.

-----------------------------------------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
int i=0, max=1; //<-- initial size of the array, which is defined in the while loop below.
string line;


ifstream ratesfile("C:\\rates.txt");

if (ratesfile.is_open()) {
while (! ratesfile.eof()) {
string *array = new string[max];
getline(ratesfile,line);
cout << line << endl;
array[i]=line;
i++;
max++;

}
ratesfile.close();
}
cout << "PRESS A KEY TO SHOUT RATES OUTSIDE THE WHILE LOOP" ;

cin.get();

cout << array[2] << endl; //<--This is not working, since the "array[]" is undeclared (although it is in the loop). Can't find a way to declare it.

cin.get();

return 0;
}

---------------------------------------------------------------

Any help would be appreciated,

Thx
Last edited on
Because youve declared the array in the loop, its life will end when the loop ends

If you declare it near string line(or somewhere else inside of main and outside of loops or if/else blocks), you can use array in the whole main function

Also;

1
2
3
4
5
6
7
8
9
while (! ratesfile.eof()) {
string *array = new string[max];
getline(ratesfile,line);
cout << line << endl;
array[i]=line;
i++;
max++;

}


What youre doing here is really, bad, and strange, wgeb you go through the loop, you allocate max strings to array, everytime after that, you allocate again, and again, overwriting what you already had, leaking memory, (and the previous string that array contained is also lost)

You should probably use a vector, or list of strings, or something else dynamic in size
(or make a really big array of strings if youre really lazy and just want it to work)
Last edited on
Thnks for your answer, well I guess I am going to switch to Vectors after all.

If I declare the array outside the loop, the max++, which is in the loop, will no more affect the array size, hence the array won't be dynamic anymore.

This is the only way I have found to make this array dynamic. Im gonna let this code be and start working on vectores, however for my understanding, is there a simple way here to declare the array outside the loop, and still make it dynamic ?

thx again
array would still be dynamic if it was outside of the while loop

for example

1
2
3
4
5
6
7
8
9
10
11
12
string *array;

if (ratesfile.is_open()) { 
while (! ratesfile.eof()) {
array = new string[max];
getline(ratesfile,line);
cout << line << endl;
array[i]=line;
i++;
max++;

}


(keep in mind that this code is still,.. wrong)
Last edited on
Nice works (badly and strangely) well!

Maybe I was too much enthusastic, there is still one tiny little issue here...

Using the above code makes it dynamic, but for some reason, when adding outside the loop:

cout << array[2] << endl;
cout << array[4] << endl;
cout << array[7] << endl;

I get nothing on the screen.

However with

cout << &array[2] << endl;
cout << &array[4] << endl;
cout << &array[7] << endl;

I do get the emplacements.

Is there something else I am missing ?

sry for insisting on that and thanks
Last edited on
you allocate max strings to array, everytime after that, you allocate again, and again, overwriting what you already had, leaking memory, (and the previous string that array contained is also lost)
Seriously the code that i posted, will run, it wont work, the code that you posted, also wont work

Use some sort of dynamic array for this, list, vector, or just append to the string
(Or get the amount of lines, then allocate doing something like array = new string[lines])
Last edited on
"(Or get the amount of lines, then allocate doing something like array = new string[lines])"

Yes was thinking of something along these lines.

So this issue is settled,

thanks again

Wissam
That was almost intelligent for a spam bot o.O
That's because it replicates existing posts of others.
Topic archived. No new replies allowed.