I am trying to read into a nested struct from a txt file. The output keeps repeating the same nested output. I attempted to nest two for loop but it didn't even read at all, my display was a blank screen. So I was able to get the file to read now but it repeats the same title and yearPub information for all entries.
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
struct Discography {
string title;
int yearPub;
};
struct Collection {
string name;
string genres;
int startYear;
int endYear;
Discography records;
};
void readFile(ifstream&, string, Collection [], int&);
const int DATA_FILE = 10;
int main() {
ifstream inputMusic;
Collection music[DATA_FILE];
Discography records;
const int DISPLAY_ALL = 1,
SEARCH_ARTIST_NAME = 2,
SEARCH_GENRE = 3,
SEARCH_TITLE = 4,
SEARCH_YEAR = 5,
QUIT_CHOICE = 6;
int choice;
int count = 0;
int numFile = count;
string nameArtist,results;
readFile(inputMusic, "My_Artists.txt", music, count);
void readFile(ifstream& inputMusic, string data, Collection music[], int &count)
{
inputMusic.open(data);
if (!inputMusic)
{
cout << "Error in opening file\n";
exit(1);
}
else
{
while (!inputMusic.eof())
{
inputMusic >> music[count].name
>> music[count].genres
>> music[count].startYear
>> music[count].endYear
>> music[count].records.title
>> music[count].records.yearPub;
count++;
}
inputMusic.close();
}
return;
};
InputFile:
MJ
Pop
1980
2020
BAD 1990
DRE
Rap
1970
2022
CRONIC 1995
EMINEM
Rap
1998
2022
ENCORE 2002
WHITNEY
R&B
1974
2008
SOMEBODY 1987
OUTPUT:
Name : MJ
Genre: Pop
Start Year: 1980
End Year: 2020
Title: BAD Year Published: 1990
----------------------------------------------------
Name : DRE
Genre: Rap
Start Year: 1970
End Year: 2022
Title: BAD Year Published: 1990
----------------------------------------------------
Name : EMINEM
Genre: Rap
Start Year: 1998
End Year: 2022
Title: BAD Year Published: 1990
----------------------------------------------------
Name : WHITNEY
Genre: R&B
Start Year: 1974
End Year: 2008
Title: BAD Year Published: 1990
----------------------------------------------------