dynamic memory read loop error
Feb 29, 2012 at 12:10am UTC
I am trying to read data into an array of structs (each with two strings and one int), and my read loop stops at the second element of the second structure. Any help on this one? It's had me stumped. Ignore the repeated statements, I haven't had time to clean this up.
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
struct Song
{
string title;
string artist;
int size;
};
int playlist_size = 1;
int memory = 25;
int choice = 0;
int use = 0;
Song playlist[playlist_size];
Song * ptr = playlist;
ifstream fin;
fin.open("input_song.txt" );
while (memory >= 0)
{
for (int i = 0; i < playlist_size; i++)
{
getline(fin, playlist[i].title);
fin >> playlist[i].artist;
fin >> playlist[i].size;
memory = memory - playlist[i].size;
cout << playlist[i].title;
cout << playlist[i].artist;
cout << playlist[i].size;
cout << memory;
if ((use + 1) > playlist_size)
{
int new_size = playlist_size + 1;
ptr = new Song[new_size];
for (int i = 0; i < new_size; i++)
{
ptr[i] = playlist[i];
}
delete [] ptr;
Song playlist[new_size];
playlist_size = new_size;
}
else
use++;
}
}
Topic archived. No new replies allowed.