Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with C++ program Having trouble reading a file when using a array with

ID: 3732089 • Letter: N

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 <iostream>
#include "MyStruct.h"
using namespace std;
// Function prototype
void LoadSongData(songType s[]);
void PrintSongData(songType s[]);

MyStruct.h


#include <iostream>
#include <string>
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 <iostream>
#include <fstream>
#include <string>
#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.

Explanation / Answer

The data in the test.txt is as given in the question:

songname
Artistone with Artisttwo
Location
2
30

In case the title and Artist name are more than one word with spaces in between them,
then the way, the data is being read will not give correct answer as we are using
infile >>....

As per the given code:

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;
}

As per the data shown for test.txt, artist name seems to be consisting of more than one
word with spaces so it is better to use getline(inFile, data) where data is a string.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote