I'm working on a project for my cs162 class and am having some trouble. For part of my project I have a .txt file with a list of song attributes seperated by delimiters that I need to read into an array of structs(.txt file elements have to be seperated by delimiters).
It reads in the struct's Title, Artist, and Album fine but it wont read in the Min or Sec. I do eventually need to loop this to read in every song. I spent 5 hours trying to troubleshoot this yesterday and am at my wit's end. I would greatly appreciate any help.
Break;Three Days Grace;Life Starts Now;3;13
Joker And The Thief;Wolfmother;Wolfemother;4;40
Satellite;Rise Against;Endgame;3;58
Come with Me Now;Kongos;Lunatic;3;31
//////////////////////////////////////////////////////////////////////
It looks like we're working on the same assignment! lol. I did a quick side-by-side and there are a couple of things different.
HUGE disclaimer. I am a beginner and should not be trusted. I just think I might have some insight since I'm working on the same program.
First, instead of in.get, I used infile.get. Because we're reading from a file, right?
Second, I'm not sure you need all of those ignore statements. I just used it once, at the end of the input statements.
Third, what I did was use cin.get() in all of the places that you put cin.ignore(). This throws out the semicolon delimiter (which I think you were trying to do with ignore?).
Hope that helps!
And if anything I'm saying is bad info I hope someone will speak up so I can learn.
Thanks and the the changes don't seem to be working sadly. If yours is working right, could you paste your version of this part so I can see what's going on?
Try this as a framework to build on. There are plenty of other ways to do this but seeing that you are using C-strings this way using strtok() to parse each line is OK.
#include <iostream>
#include <string>
#include <ios> // for streamsize
#include <limits> // for include numeric_limits.
#include <fstream>
struct Song
{
std::string Title;
std::string Artist;
std::string Album;
int Min;
int Sec;
};
int main()
{
constint totalOfElemFile = 4;
Song song[totalOfElemFile];
std::ifstream in;
in.open("roster.txt");
if (!in) return(1);
for (int count = 0; count < totalOfElemFile; count++)
{
std::getline(in, song[count].Title, ';');
std::getline(in, song[count].Artist, ';');
std::getline(in, song[count].Album, ';');
in >> song[count].Min;
in.ignore(std::numeric_limits<std::streamsize>::max(), ';');
in >> song[count].Sec;
}
for (int count = 0; count < totalOfElemFile; count++)
{
std::cout << song[count].Title << std::endl;
std::cout << song[count].Artist << std::endl;
std::cout << song[count].Album << std::endl;
std::cout << song[count].Min << std::endl;
std::cout << song[count].Sec << std::endl;
}
in.close();
return 0;
}
Output...
Break
Three Days Grace
Life Starts Now
3
13
Joker And The Thief
Wolfmother
Wolfemother
4
40
Satellite
Rise Against
Endgame
3
58
Come with Me Now
Kongos
Lunatic
3
31
0 Break;Three Days Grace;Life Starts Now;3;13
0 Break
1 Three Days Grace
2 Life Starts Now
3 3
4 13
1 Joker And The Thief;Wolfmother;Wolfemother;4;40
0 Joker And The Thief
1 Wolfmother
2 Wolfemother
3 4
4 40
2 Satellite;Rise Against;Endgame;3;58
0 Satellite
1 Rise Against
2 Endgame
3 3
4 58
3 Come with Me Now;Kongos;Lunatic;3;31
0 Come with Me Now
1 Kongos
2 Lunatic
3 3
4 31
****************** Bingo
"Three Days Grace" by Life Starts Now Length: 3:13
"Wolfmother" by Wolfemother Length: 4:40
****************** Bingo
"Rise Against" by Endgame Length: 3:58
****************** Bingo
"Kongos" by Lunatic Length: 3:31
Program ended with exit code: 0
Thanks, I've been fooling around with strcmp but I can't seem to convert an inputed name or my song[i].Artist into a char a[] value like the syntax for it shows.
Thanks and I finally figured it out. I had to use strcpy to convert. Its all working now except for the loop. I tried nesting a do..while loop inside a for loop so that i would increment and the strcmp value would stop at the correct value but it just loops infinitely.
void searchartist(int &listsize, int &i, Song song[])
{
listsize=i;
i=0;
cout<<"Please enter the name of the song's artist: ";