-8

I'm pretty new to programming so this might be a question with an obvious answer for you guys and I'm really stumped, why doesn't cout work in the following function? I've included the iostream header so I'm assuming it has something to do with it being in the function?

 int inputFileData(song musicLibrary[])
{
    char discard = ' ';
    int counter = 0, length = 0;
    ifstream inData;

    inData.open("songs.txt");

    if (!inData)
    {

        cout << "Error: No input file found " << endl;
        cout << "Program terminating..." << endl;
        return 1;
    }

    while (!inData.eof())
    {
        inData.getline(musicLibrary[counter].title, ARRAY_CONST, ';');
        inData.getline(musicLibrary[counter].artist, ARRAY_CONST, ';');
        inData >> musicLibrary[counter].durationMinutes;
        inData.get(discard);
        inData >> musicLibrary[counter].durationSeconds;
        inData.get(discard);
        inData.getline(musicLibrary[counter].album, ARRAY_CONST, '\n');

        length = strlen(musicLibrary[counter].album);

        if (length = 0)
        {
            cout << length << endl; //This cout object doesn't work in this function
            break;
        }

        else
            counter++;
    }

    return counter;
}
David
  • 61
  • 6
  • `if (length = 0)` -- use `==`. – Eli Oct 22 '17 at 10:10
  • 1
    Mandatory read: [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo Oct 22 '17 at 10:11
  • Don’t use a array, use std::vector. Don’t use char arrays, use std::string. –  Oct 22 '17 at 10:12
  • OH WOW! I can't believe I missed == :)). This is one of my class projects and we can't use the string class, and we haven't reached vectors yet. – David Oct 22 '17 at 10:21

2 Answers2

3

The line if (length = 0) should be if (length == 0).

Eli
  • 693
  • 5
  • 12
1

Elaborating Eli's answer:

if (length = 0)

Assigns the value 0 to length and then evaluates the expression. Since the expression returns 0 the condition evaluates to false and you don't enter the if clause.

Instead, use if (length == 0)

Daniel Trugman
  • 6,100
  • 14
  • 35