By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

Reading data into struct

P: 1
Hello all,

I have an assignment where I have to take a text file of music albums and read them into a struct, then alphabetize the songs within each album and then alphabetize the albums. My problem is getting the data read into the struct. I think if I can pass that hurdle, I can do the sorting. Here is the structure of the input file.

Name of Singer #1
Name of Singer #2
Name of Singer #3

1. Song #1
2. Song #2
3. Song #3



The name of the album is in all caps, the artists are one per line, the songs each have a number in front of them and each album is separated by a row of equal signs.

Now, here are the limitations.

We have not covered vectors yet (don't ask me why) so I'm confined to using arrays and structs. We have not covered regular expressions, and so I have ABSOLUTELY no clue how to check for things like "if a line is all caps then read it into album[0].title". Furthermore I don't know how to increment my loop counter so that everytime I hit a line of equal signs I read the next block of lines into my next struct.

I understand you aren't here to do my homework for me. Please help me at least get data read into the struct.

Mar 21 '07 #1
Share this Question
Share on Google+
1 Reply

P: 99
well, i think you should start by writing a loop that will take every input line in succession.

once you have an input line in a char[], strstr() will search for substrings in your input.

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.   char example[] ="This is a simple string";
  4.   char * result;
  5.   result = strstr (example,"simple");
  6.   cout << result;
  7.   system("PAUSE");
  8.   return 0;
  10. //this will output "simple string"
  11. }
you can use the strstr() as the conditional for an if statement. it will return 1 if the match succeedes . . . so

Expand|Select|Wrap|Line Numbers
  1. if(strstr(example,"========"))
  2. {
  3. <next record>
  4. }
you can search for lower case letters to know that its is an artist's name and you will know that the first entry is the album name. any line after the first will be an artist, and then after a blank line, are song names.
Mar 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.