Creating an Array of objects and printing them

Hello! This forum has been awesome. It's helped me a lot. I have a problem you have probably seen before, and looking in the forums has gotten me along a lot farther than I would have alone.

However, I seem to be stuck on something simple. I have to make a DVD object, create an array of 10 of these objects. Then print them out. A previous thread helped me get to this spot (with some of my own personal changes).

However, it doesn't seem to print out anything when I try to print the array. Other than 10 blank lines.

1. The first if condition works, via personal testing.
2. The first for loop, to create the array, iterates 10 times and does not break out early, personally tested.
3. The second for loop, to print the array, iterates 10 times, but prints blank lines, personally tested.

The error seems to be in importing my DVD objects into my dvdArray. The array does seem to actually be empty. Anyways, I can't look at it anymore right now. going cross-eyed.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class DVD {

	string details;				// temporary variable for testing purposes.
//	string title;				// Name of the movie
//	int runtime; 				// Runtime in minutes
//	int year;					// Copyright year
//	string genre;				// Main genre of movie
//	float rating;				// Metascore rating
//	string tagline;				// One sentence tagline for movie
//	string MPAA;				// Movie rating
public:
	DVD(); 					// Constructor
	~DVD();					// Deconstructor
 	void importFile(ifstream &importFile);		// Import DVD data	
	void printDVD();					// Print the DVD Information
};

DVD::DVD() : details{""} 				{	// Constructor
}

DVD::~DVD() {							// Deconstructor
}

void DVD::importFile(ifstream &datafile) {		// Add details to a DVD object
}

void DVD::printDVD() {
	cout << details << endl;				// Print a DVD object.
}

string filename; 					// filename variable for user input

int main() {
	
	cout << "Please enter file name: ";	// Request from user the name of a file to count words.
   	cin >> filename; 				// Assigns the user input to the string variable filename.
	ifstream datafile(filename); 		// Reads the users file name into datafile

	DVD dvdArray[10];				// Create an array to hold DVD objects.  
	
	if(!datafile.fail()) {				// If does not fail to open, then execute the for loop.
		for(int i = 0; i < 10; i++) {	// for loop to execute 10 times.
    		dvdArray[i].importFile(datafile);	// Uses the importFile function to import DVDs into the dvdArray.
    		if (datafile.eof()) break;		// If this is the end of the file, break out of the for loop.
		}
	}
	else {
	cout << "Error!  Does this file exist?" << endl;	// Error statement if file fails to open.
	}
	
	for(int i = 0; i < 10; i++) dvdArray[i].printDVD();	//	Print the array of DVDs.

}


Anyways, I do plan on doing more aesthetic changes, but only once I get the program functional. Not quite there yet... feels so close.
Last edited on
Wow that formatting is horrible. I'll try to fix that a bit!
However, it doesn't seem to print out anything when I try to print the array. Other than 10 blank lines.

Hmm, what do you expect?
Your import function isn't doing anything and your details is a empty string.
Set your details to some dummy text and see if it prints it.
Got it! Thank you thank you!

Learning about getline. lol. Now I wanna play around with it. I imagine there has to be away to comma seperate this stuff with getline.
Yes sure, try this:
1
2
3
4
  stringstream ss("One,two,three"); // #include <sstream>
  string line;
  while(getline(ss, line, ','))
    cout << line << "\n";

Topic archived. No new replies allowed.