Help with using structs

Hi all,

I have an assignment that requires me to do this:
Create an abstract data type that represents a DVD in this store. Consider all the data and operations that may be necessary for the DVD type to work well within a rental management system. Include a print() member function that displays all the information about the DVD. Test your data type by creating an array of ten DVD instances and filling them using information read from a test input file that you create. Display the DVD information.

I have been trying to get this to work here, but maybe I just am not quite understanding how to use what I am trying to use properly. Or at all.
My code is posted below. The information in the text file is 10 lines that are like:
Movie1;R;Horror
Movie2;G;Comedy
etc..

Any help is appreciated. If you think I am approaching my assignment in the wrong way, let me know.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct DvdInfo
{
	string info[9];	
};

void print(DvdInfo all)
{
	cout << all.info[9] << "\n";	
}

int main()
{
	ifstream fileIn("TMA2Question1.txt");
	if(fileIn.is_open())
	{
		int lineCount = 0;
		string dvds;
		
		for(int i =0; i < 10; ++i)
		{
			fileIn >> dvds[i];
			lineCount++;
		}
		DvdInfo dvds2 = dvds;
	}
	
	print(dvds);

	return 0;
}
You should probably be creating a DVDInfo class (containing data and member functions), rather a C-style struct. Also, you probably want a string for each type of DVD information. For example:

1
2
3
4
5
6
7
8
9
10
class DVDInfo
{
   public:
      DVDInfo(const string &_title, const string &_rating, const string &_genre);
      print();
   private:
      string title;      // e.g. Movie1, Movie2
      string rating;   // e.g. R, G
      string genre;   // e.g. Horror, Comedy
};


Then in your main function create a std::vector or std::list of DVDInfo objects & and as you read in each line of the file, create a new DVDInfo object & append it to your vector or list. Finally, iterate through the vector or list, calling each DVDInfo objects' print() function.

Unfortunately your file-reading code will have to get quite a bit more complicated for this to work...
Thanks for your reply.
I wasn't quite sure how to implement this, and I can't quite get this to work... maybe I am doing it wrong still, could you be so kind as to take a look at let me know?
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
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class DVDInfo
{
	private:
	string name;
	string genre;
	string rating;
	
	public:
	void print()
	{
		cout << name << " - " << genre << " - " << rating << " - " << stars << "\n";
	}
}

int main()
{
	string line;
	string temp;
	ifstream myfile("file.txt");
	if(myfile)
	{
		while(getline(myfile, line))//parse a single line
		{ 
			stringstream lineStream(line);
			int count = 0;
			while(getline(lineStream, temp, ','))
			{
				if(count == 0)
				{
					DVDInfo.name = temp;
				}
				else if(count == 1)
				{
					DVDInfo.genre = temp;
				}
				else if(count == 2)
				{
					DVDInfo.rating = temp;
				}
				count++;
			}
			count = 0;    
		}
		myfile.close();
	}
	print();
	else
	{
		cout<<"No File bro";
	}
	return 0;
}
I actually spent some time since my last post just trying to figure out what was wrong.
I got it -sort- of working, but let me show you, and explain my new problem:

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

using namespace std;

class DVDInfo
{
	public:
	string name;
	string genre;
	string rating;
	
	void print()
	{
		cout << name << " - " << genre << " - " << rating << "\n";
	}
};

int main()
{
	string line;
	string temp;
	ifstream myfile("file.txt");
	DVDInfo storage;
	if(myfile)
	{
		while(getline(myfile, line))//parse a single line
		{ 
			stringstream lineStream(line);
			int count = 0;
			while(getline(lineStream, temp, ','))
			{
				if(count == 0)
				{
					storage.name = temp;
				}
				else if(count == 1)
				{
					storage.genre = temp;
				}
				else if(count == 2)
				{
					storage.rating = temp;
				}
				count++;
			}
			count = 0;    
		}
		myfile.close();
		storage.print();
	}
	
	else
	{
		cout<<"No File bro";
	}
	return 0;
}


This now compiles. But it only outputs the first last line of the text file. What have I done wrong?

Edit: I have realized that I don't have an array for storing all of the dvd instances... but I am not quite sure how to implement one. I have tried a few things to no avail, and hundreds of lines of error spam when trying to compile with everything I tried to do.
Last edited on
I got it working, thanks for help in any case!
Topic archived. No new replies allowed.