Read Array into Struct

Hi!
I'm trying to write a program that has a struct of dvd info that I have written below. I then have a text file that reads into a array (dvdArray) I then need to figure out a way to read the dvdArray into the dvd stuct and then print it out. I'm not sure how to separate the information in dvdArray and then read it into the different parts of the dvd struct

Thanks for the help!

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

struct dvd{
		string name;
		string genre;
		string rating;
		int released;
		string renter;
		string rentday;
	}dvd1, dvd2;

	void print(dvd movie){
		cout <<"Title: " << movie.name << endl;
		cout <<"Genre: " << movie.genre << endl;
		cout << "Rating: " << movie.rating << endl;
		cout << "Release date: " << movie.released << endl;
		cout << "Name of renter: " << movie.renter << endl;
		cout << "Date rented: " << movie.rentday << endl;
	}
int main() {
	string dvdArray[10];
	ifstream file("DVD.txt");

	if(file.is_open()){
		int i = 0;
		while(i <10 && getline(file, dvdArray[i++]));

		
	}
	
return 0; 

}

I think you are looking for this

http://www.cplusplus.com/reference/istream/istream/read/

hope it helps
Can't make suggestions as you haven't shown the format of the dvd text file.
That's true, here's the format of the text file:
Mean girls; comedy; PG; 2009; Regina George; 12.07.2015;
The Conjuring; Horror; R; 2013; Sara Johnson; 16.05.2016;
Pokemon 2000; Kids; G; 2000; Ash Katchem; 15.04.2016;
etc...
Since each field is deliminted with a ; that makes it easy to use getline.

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

struct dvd
{   string name;
	string genre;
	string rating;
	string released;    // eaasier as a string
	string renter;
	string rentday;
	
	//  Member functions
	void print ();
	bool read (ifstream & file);
};

void dvd::print ()      
{   cout <<"Title: " << name << endl;
	cout <<"Genre: " << genre << endl;
	cout << "Rating: " << rating << endl;
	cout << "Release date: " << released << endl;
	cout << "Name of renter: " << renter << endl;
	cout << "Date rented: " << rentday << endl;
}

bool dvd::read (ifstream & file)
{   getline (file, name, ';');
    getline (file, genre, ';');
    getline (file, rating, ';');
    getline (file, released, ';');
    getline (file, renter, ';');
    getline (file, rentday, ';');
    return file.good();  // true if all the getlines were successful
}
    
int main() 
{   vector<dvd>     dvds;       //  A collection of dvds 
    dvd             dvd1;       //  A temporary for reading a dvd
	ifstream file("DVD.txt");
    
	if(! file.is_open())
	{   cout << "failed to open input file" << endl;
	    return 1;
	}
	while (dvd1.read(file))
	{   dvds.push_back (dvd1);
	}

`   return 0; 
}


Okay so I did what you said and it kind of worked.
It print out like this:
Title: PG
Genre: 2013
Rating: Steven Rogers
Release date: 15.04.2016
Name of renter:

Date rented: Action

So it's not organized properly and that's not the first line of text. in the file, it's actually the last. I think it's cause I'm not using a array which is what I would prefer to do.

Thank for the help!
If you're not using the code I posted, then I would need to see your current code.
I'm basically using the same code you wrote, but I want to use an Array instead of a vector and I'm not sure how to do that

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
61
62
63
64
//============================================================================
// Name        : TMA2Question1.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

struct dvd{
		string name;
		string genre;
		string rating;
		string released;
		string renter;
		string rentday;

		void print();
		bool read(ifstream & file);

};

void dvd::print(){
		cout <<"Title: " << name << endl;
		cout <<"Genre: " << genre << endl;
		cout << "Rating: " << rating << endl;
		cout << "Release date: " << released << endl;
		cout << "Name of renter: " << renter << endl;
		cout << "Date rented: " << rentday << endl;
	}
bool dvd::read(ifstream & file)
{
	getline(file, name, ';');
	getline(file, genre, ';');
	getline(file, rating,';');
	getline(file, released, ';');
	getline(file, renter, ';');
	getline(file, rentday, ';');
	return file.good();
}
int main() {
	vector<dvd> dvds;
	dvd			dvd1;
	ifstream file("DVD.txt");

	if(! file.is_open()){
		cout << "Failed to find input file" << endl;
		return 1;
	}
	while(dvd1.read(file))
	{ dvds.push_back(dvd1);
	}

	dvd1.print();


return 0;

}



So what I would like to do is have read the text file into the Array and from there read the Array into the struct. So instead of the text file reading into the vector I need it to read into the array and from there read the first line of the array (dvdArray[1]) into struct dvd and then print out that information using print function and then loop that until dvdArray[10] is read into struct dvd!

Thank you so much for your help! :)
Last edited on
Topic archived. No new replies allowed.