Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. How we can improve our listening skills?

Answered: 1 week ago