Question: Need help with C++ program Having trouble reading a file when using a array with a struct. Here is my code so far prototypes.h #include

Need help with C++ program

Having trouble reading a file when using a array with a struct.

Here is my code so far

prototypes.h

#include #include "MyStruct.h" using namespace std; // Function prototype void LoadSongData(songType s[]); void PrintSongData(songType s[]);

MyStruct.h

#include #include using namespace std;

//Define the structure

struct artistType { string Name; string CountryOfOrigin; };

struct timeType { int Minutes; int Seconds; };

struct songType { string Title; Artist Artist; Time Length; };

track.cpp

#include #include #include #include "prototypes.h" #include "MyStruct.h" using namespace std;

// load song data from file void LoadSongData(songType s[]) {

// input file stream ifstream infile; // opening filename infile.open("mysongs.txt"); //loop until count for (int count = 0; count < 5; count++) { infile>> s[count].Title >> s[count].Artist.Name >> s[count].Artist.CountryOfOrigin >> s[count].Length.Minutes >> s[count].Length.Seconds; } } // Define the function ShowSongData void PrintSongData(songType s[]) { for (int i = 0; i < 5; i++) { cout << s[i].Title << endl; cout << s[i].Artist.Name << endl; cout << s[i].Length.Minutes << endl; cout << s[i].Length.Seconds << endl; } }

text.txt

songname Artistone with Artisttwo Location 2 30 Songtwo OneArtist Location2 3 32 SongThree ArtistOne and Aritisttwo Location3 4 11

My question is in track.cpp, If i put my code into a outfile or cout what is being read it does not match the example text.

it just prints the couple of strings and the rest is lost due to the the space inbetween the words which leads to the rest trying to match a string to a integer like this

songname Artistone with -8521312 -2321313

How do I use a array to read a file from a struct and load the file contents properly and print them out on screen properly on screen?

The code I have is simiplified so it will not run however I can paste my full code if needed.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!