1

These are my Structs:

struct Artist
{
    string Name;
    string CountryOfOrigin;

};

struct Time
{
    int Minutes;
    int Seconds;
};

struct Song
{
    string Title;
    Artist ArtistDetails;
    Time LengthOfSong;
};

And my Function:

void LoadSongDataFromFile(Song s[])
{
    string inputFile, title, name, country;
    int minutes, seconds;
    cout << "Please enter the input file name: ";
    cin >> inputFile;

ifstream input;
input.open(inputFile);

int count = 0;
while (input >> title)
{
    s[count].Title >> title;
    s[count].ArtistDetails.Name >> name;
    s[count].ArtistDetails.CountryOfOrigin >> country;
    s[count].LengthOfSong.Minutes >> minutes;
    s[count].LengthOfSong.Seconds >> seconds;

    count++;
}

}

I'm getting an error in these three lines:

    s[count].Title >> title;
    s[count].ArtistDetails.Name >> name;
    s[count].ArtistDetails.CountryOfOrigin >> country;

Saying no operator >> matches these opperands. Opperand types are: std::string >> std::string

Also the data I'm trying to put into the struct array comes from a text file which contains this information:

Perfect

Ed Sheeran with Beyonce

England

4

23

The text file name is songdata.txt if that matters. Any help is greatly appreciated!

triolo
  • 11
  • 1

2 Answers2

1

You can use = operator to assign values.

input >> minutes;
s[count].LengthOfSong.Minutes = minutes;

Or read directly into the struct:

input >> s[count].LengthOfSong.Minutes;

Reading with >> reads one word from the input, so it will only work with your numbers. To read a complete line (the strings), use std::getline.

VLL
  • 9,634
  • 1
  • 29
  • 54
0

The >> operator has two meanings:

  • shift bits around
  • read input from a stream into an object

The latter meaning is used here. As you see, the definition says "from a stream" and "to an object".

In your code you call the >> operator to read "from a string" s[count].Title to another string title.

There are many variants for the predefined >> operator. They all have a stream as the first operand. Therefore, to use them, use std::cin >> s[count].Title.

As mentioned in the other answer, the >> operator stops copying after the first word. Therefore it's better to use std::getline(std::cin, s[count].Title).

Roland Illig
  • 40,703
  • 10
  • 88
  • 121