0

Having trouble compiling program. Should read input from text file and store into a dynamic array of structure. I'm prettty sure my problem lies in the ipod_Initialize function. I'm not sure how to pass the dynamic array structure through the function.

#include <iostream>
#include <string>
#include <fstream>
# include <ctime>

using namespace std;
struct Song
{
    string title;
    string artist;
    float size{};
};
int song_Count(string);
void ipod_Initialize(string, int, Song);
void show_playlist();
int main(char *argv[]) {
    // argv is filename
    int count = song_Count(argv[1]);
    Song* songInfo = new Song[count];
    ipod_Initialize(argv[1], count, *songInfo);
    cout << songInfo[0].title;
    delete[] songInfo;
    return 0;
}
int song_Count(string fileName){
    ifstream inFile;
    string data;
    int count = 0;
    inFile.open(fileName);
    if (!inFile.is_open()){
        cout << "Error opening file" << endl;
        return 0;
    }
    while(!inFile.eof()){
        getline(inFile, data);
        if (!data.empty()){
            count++;
        }
    }
    inFile.close();
    count = count / 3 + 1;
    return count;
}
void ipod_Initialize(string fileName, int count, Song* songInfo) {
    string data;
    ifstream inFile;
    inFile.open(fileName);
    for (int i = 0; i <= count; i++) {
        getline(inFile, data);
        if (!data.empty())
            songInfo[i].title = data;
        getline(inFile, data);
        if (!data.empty())
            songInfo[i].artist = data;
        //getline(inFile, mem);
        if (!data.empty())
            inFile >> songInfo[i].size;
    }
}

  • 1
    Rather than dynamically allocating an array, have you tried `std::vector`? – Thomas Matthews Mar 03 '20 at 23:17
  • 1
    Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Mar 03 '20 at 23:21
  • 2
    What compilation error are you getting? – JohnFilleau Mar 03 '20 at 23:21
  • 1
    Unrelated: The range of `i` in `for (int i = 0; i <= count; i++)` will be [0, `count`] for a total of `count`+1 iterations. This is one larger than the size of the allocated array. – user4581301 Mar 03 '20 at 23:23
  • 1
    `int main(char *argv[])` should be `int main(int argc, char *argv[])`. Under normal circumstances `main` can only be `int main()` or `int main(int argc, char *argv[])` – user4581301 Mar 03 '20 at 23:26
  • 1
    This is probably the one the compiler, linker really, is up in arms about : `void ipod_Initialize(string, int, Song);` does not match `void ipod_Initialize(string fileName, int count, Song* songInfo)`, nor does either match the usage `ipod_Initialize(argv[1], count, *songInfo);`. – user4581301 Mar 03 '20 at 23:30
  • Fixed it. Pointer should be in the function definition and not the call. `void ipod_Initialize(string, int, *Song);' and `ipod_Initialize(argv[1], count, songInfo);` **not** `void ipod_Initialize(string, int, Song);` and `ipod_Initialize(argv[1], count, *songInfo);` – Miles Banister Mar 04 '20 at 01:39

0 Answers0