create one array that contains multiple data types from a data file

I have to make an inventory for a CD store and i need to make an array to store all the data. I have to take the information from a seperate data file that looks like this:



101 3
2
25.99
20.99
500
Paige Miller
The Wings of Freedom
105 1
3
19.99
15.99
11.99
75
Michael Palmer
Smooth Sounds
150 2
4
15.99
12.99
11.99
9.99
27
Beethoven
Sounds of Fall


The numers and names mean:
ASIN (a string)
Music Category (1,2,3, or 4)(integer between 1-4)
# of selling prices price history (max 4 real numbers with 2 decimal places)
balance on hand (integer)
Artist Name (string)
CD Title (string)
: : : :
//next CD record


I dont know how to write the information from the data file to a single array that consists of multiple data types. I need to be able to seach the array for information as well so it has to make sence. Any help would be nice.
Very Respectfully
Create your own record for the data, and store it in one of the standard containers.

1
2
3
4
5
6
7
8
9
10
11
struct MusicCD_t
  {
  string         ASIN;
  unsigned       category;
  deque <double> price_history;
  unsigned       inventory;
  string         artist_name;
  string         cd_title;
  };

typedef deque <MusicCD_t> MusicCDList_t;

Next, you need to create functions to read and write this information. The standard << and >> stream operators are convenient, but not necessary. I find them useful:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
istream& operator >> ( istream& ins, MusicCD_t& cd )
  {
  // Read a SINGLE MusicCD_t from the input stream.
  // Extraction operators are always a bit tricky. This one is fairly simple.
  try
    {
    string s;

    // ASID
    getline( ins, cd.ASID );

    // Category
    getline( ins, s );
      {
      istringstream ss( s );
      ss >> cd.category;
      if (!ss.eof()) throw 1;
      if ((category < 1) || (category > 4)) throw 1;
      }

    while (getline( ins, s ) && (s.find( '.' ) != string::npos))
      {
      istringstream ss( s );
      double d;
      ss >> d;
      if (!ss.eof()) throw 1;
      cd.price_history.push_back( d );
      }

    // Remember that s was read already...
    if (!ins) break;
      {
      istringstream ss( s );
      ss >> cd.inventory;
      if (!ss.eof()) throw 1;
      }

    getline( ins, cd.artist_name );

    getline( ins, cd.cd_title );
    }
  catch (int)
    {
    ins.setstate( ios::bad );
    }

  return ins;
  }

istream& operator >> ( istream& ins, MusicCDList_t& cd_list )
  {
  // Read as many MusicCD_t records from stream as possible.
  // This is much easier, since all the hard work was done above.
  MusicCD_t cd;
  while (ins >> cd)
    {
    cd_list.push_back( cd );
    }
  return ins;
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ostream& operator << ( ostream& outs, const MusicCD_t& cd )
  {
  // Write a SINGLE MusicCD_t to the output stream.
  // Fortunately, this is very easy.
  outs << cd.ASID << "\n";
  outs << cd.category << "\n";
  for (unsigned n = 0; n < cd.price_history.size(); n++)
    outs << cd.price_history[ n ] << "\n";
  outs << cd.inventory << "\n";
  outs << cd.artist_name << "\n";
  outs << cd.cd_title << "\n";
  return outs;
  }

ostream& operator << ( ostream& outs, const MusicCDList_t& cd_list )
  {
  // Write all the MusicCD_t records in the list to stream.
  // Again, very easy.
  for (unsigned n = 0; n < cd_list.size(); n++)
    outs << cd_list[ n ];
  return outs;
  }


Search functions simply require you to look through the list for the proper information.
1
2
3
4
5
6
7
8
9
10
11
12
MusicCDList_t find_category( unsigned category, const MusicCDList_t& cd_list )
  {
  MusicCDList_t result;
  for (unsigned n = 0; n < cd_list.size(); n++)
    {
    if (cd_list[ n ].category == category)
      {
      result.push_back( cd_list[ n ] );
      }
    }
  return result;
  }

Hope this helps.

Crud. Now that I typed that all up, I sure hope this isn't homework of some sort.
(If it is, ask specific questions and we will help. If you use this, your teacher will know you didn't figure this out yourself. If he is a good teacher, he'll give a failing grade for it. If it is not homework, you're in luck.)
Topic archived. No new replies allowed.