Hi, I'm new to the C++ community hope you guys can give me some guidance.

The program I'm making is suppose to read information from a file called music.txt

Inside this file there is information about songs in the following format:

Bandname, Songname, Duration of the Song,

Here a partial extract of the file:
AC/DC,Dirty Deeds Done Dirt Cheap,303,
Alison Moyet,All Cried Out,410,
Allan Browne Quintet,Cyclosporin,291,
The Angels,Take A Long Line,180,
Architecture In Helsinki,Maybe You Can Owe Me,242,

The code I've come up with so far should suggest that there is:
1. An array of 62 songs.
2. Struct for each song to organize all 3 pieces of information.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

struct songType
{
    string bandName;
    string songName;
    int duration;
};

songType songs[62];

int main()
{
    ifstream infile;
    infile.open ("music.txt");
    string line;

    for (int counter = 0; counter < 62; counter++)
    {
        infile >> songs[counter].bandName = getline(infile, line, ',');
        infile >> songs[counter].songName = getline(infile, line, ',');
        infile >> songs[counter].duration = getline(infile, line, ',');
    }
    return 0;
}

Here is the problem, I get the following error:

Description Resource Path Location Type
C:/Users/Shuda/Desktop/C++/Eclipse/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/ios_base.h `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private Test 784 C/C++ Problem within this context.

This appears at:

infile >> songs[counter].bandName = getline(infile, line, ',');

Does anyone have any suggestions about what I'm doing wrong?

Recommended Answers

All 9 Replies

You are trying to assign the return value of getline to a type variable of type 'string'.
The getline function takes an input stream as it's first parameter, like you have given it, and the string that you would like to assign the extracted value to as the second parameter. The third (optional) parameter is a delimiter, if you specify a delimiter getline will read up until it finds the delimiter.
So given the line: Alison Moyet,All Cried Out,410, in your file, the following code: getline(infile, line, ','); would result in the string 'line' containing: Alison Moyet So, what you need to do is to read in the entire line (i.e. specify no delimiter as it will default to the newline character). Then the variable 'line' will contain the entire line with the commas and all you need to do is split it.

Yes, that line is having the exact problem.
change infile >> songs[counter].bandName = getline(infile, line, ','); to getline(infile, songs[counter].bandName, ','); And similarly for each .songName and .duration
Why?
As your files are comma-separated, there is no point in using the input operator >> . This operator will fetch only a 'word' (word means a string which is delimited by a white-space).

>So, what you need to do is to read in the entire line (i.e. specify no delimiter as
>it
>will default to the newline character). Then the variable 'line' will contain the
>entire line with the commas and all you need to do is split it.
No. He is doing it rightly. As the file is comma-separated, he don't have to parse every single line(that is one advantage by maintaining the uniform syntax). He just need to earase the infile>> from all those lines.

songs[counter].bandName = getline(infile, line, ',');

didn't work, it just came up with some error.

I thought through it and I decided to use the following approach:

getline(infile, songs[counter].bandName, ',');

so far it seems to work like a charm.

Thanks for the help tho, I had forgotten >> only reads one word delimited by a space.
I've been taking >> for granted with cin as cin only requires you to enter one value at a time and I ended up forgetting what >> meant.

No. He is doing it rightly. As the file is comma-separated, he don't have to parse every single line(that is one advantage by maintaining the uniform syntax). He just need to earase the infile>> from all those lines.

No, you cannot assign the return type of getline to a string.

I didn't notice the commas on the ends of each line though I suppose even without them you could just modify the final getline call to have no delimiter.

In any case a: getline( infile, string, ',' ); where string is the attribute you want to read in, will suffice.
And if you didn't have commas on the end when you read in the last attribute you would simply do the same as above but without specifying a delimiter.

You are trying to assign the return value of getline to a type variable of type 'string'.
The getline function takes an input stream as it's first parameter, like you have given it, and the string that you would like to assign the extracted value to as the second parameter. The third (optional) parameter is a delimiter, if you specify a delimiter getline will read up until it finds the delimiter.
So given the line: Alison Moyet,All Cried Out,410, in your file, the following code: getline(infile, line, ','); would result in the string 'line' containing: Alison Moyet So, what you need to do is to read in the entire line (i.e. specify no delimiter as it will default to the newline character). Then the variable 'line' will contain the entire line with the commas and all you need to do is split it.

I'm sorry I wasn't specific enough, what I wanted to do is to store everything into the arrays so I can call them up later for various purposes.

To me the easiest way is to split them up and store them. Rather than store them then split them as you seem to be suggesting. Thanks tho.

No, you cannot assign the return type of getline to a string.

I didn't notice the commas on the ends of each line though I suppose even without them you could just modify the final getline call to have no delimiter.

In any case a: getline( infile, string, ',' ); where string is the attribute you want to read in, will suffice.
And if you didn't have commas on the end when you read in the last attribute you would simply do the same as above but without specifying a delimiter.

Of course, you cannot assign it to a string. (see my post). But I was emphasizing on the point that you don't need to read a whole line and then parse it. Instead you can just put the the comma as the third argument.

I'm sorry I wasn't specific enough, what I wanted to do is to store everything into the arrays so I can call them up later for various purposes.

To me the easiest way is to split them up and store them. Rather than store them then split them as you seem to be suggesting. Thanks tho.

You're bang on with your above solution ;) Good to know it's working.

No, you cannot assign the return type of getline to a string.

I didn't notice the commas on the ends of each line though I suppose even without them you could just modify the final getline call to have no delimiter.

In any case a: getline( infile, string, ',' ); where string is the attribute you want to read in, will suffice.
And if you didn't have commas on the end when you read in the last attribute you would simply do the same as above but without specifying a delimiter.

I use ',' because '\n' is the default delimiter. I need it to be a comma because I can then store all three pieces of information seperately.

I use ',' because '\n' is the default delimiter. I need it to be a comma because I can then store all three pieces of information seperately.

The thing to remember is that if your input is: Alison Moyet,All Cried Out,410 instead of (what i read it as originally ;P): Alison Moyet,All Cried Out,410, Then the last parameter would not find a ',' as a delimiter. So if you get strange output that's a good thing to check. And if it is necessary to not have a comma at the end of the line on an input file you can just call the getline in the same way with no delimiter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.