Writing from file to structure

Hello everyone! :D

So I have this .txt file:
1 - The Witcher - 2015
2 - Batman - 2015
3 - XV - 2016

And my purpose is to put every element on the file, inside a structure.
So here's my code 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
#include <iostream>
#include <fstream>	
#include <string>
#include <sstream>		
#include <iomanip>		
#include <vector>

using namespace std;

int main()
{
	string file_name;

	cout << "Choose your file: ";
	getline(cin, file_name);

	struct WantList
	{
		int game_number;
		string game_name;
		int launch_year;
	};

	vector <struct WantList> list;
	struct WantList registo;

	ifstream file;
	file.open(file_name.append(".txt"));

	string lines_read;

	if (file.is_open())
	{
		while (getline(file, lines_read))
		{
			string garbage;
			istringstream instr(linhas_lidas);

			instr >> registo.game_number >> garbage>> registo.game_name >> garbage >> registo.launch_year;
			list.push_back(registo);
		}
	}
        cout << ?????;
}


My question is: what do I write in the spot with all the ????????? to make the console write "Batman"?

Thanks in advance! :D
use the dot operator to access stucture members.
1
2
3
4
5
6
7
8
9
struct Test
{
    int x;
};
...
Test t;
...
cout<<t.x<<endl;
..
And create the structure above main, not inside.

https://www.youtube.com/watch?v=VMFKz7Klx7I&ab_channel=thenewboston
I know that's how I acess the structure elements, but if I do something like:

 
cout << list[1];


is gives me an error message.
Is there because I've used "list.push_back(registo)"?

TarikNeaj, I would still prefer to create the structer inside main, that's how it was teatched (for now) to me.
Last edited on
I thought you said you know how to access the structure element??
list[1] is not an structure member, its a struct.
you have to index the structure not the vector.
That's why I'm having problems with this. I know how you normally acess the structure, but since I want to acess the vector, I don't know how to do it... :(

You're talking something like this?
 
	cout << lista[registo.nome_jogos[1]];

or
 
       cout << registo.game_name;    //but this only display the last one, which is "XV"... 


I've tried something like this to, and I know that many things I've tried are stupid, but I seriusly have no ideia what to try anymore...

Last edited on
if list is a vector of structs, to access the member of the struct inside the vector you do:

list[index].membername

ex:
cout << list[0].game_name << endl;
Last edited on
Oh, thank you so, so much! :D
I was getting crazy trying to do that!

And if it's not much to ask, is it possible to use that vector on a function? And how do you do it, because what I tried isn't working... :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void test(vector <struct WantList> name)
...

int main()
{
   struct WantList
	{
		int game_number;
		string game_name;
		int launch_year;
	};

	vector <struct WantList> list;
   ...
   test(list);
   ...
}
Last edited on
As it is now, Your struct is only available in the main function scope you can not access it outside of main . i would advice you declare your struct above main to avoid futher headaches.
Last edited on
Ok, I've followed your advice!

I have all my questions answered. Thank you all for helping me, you've been awesome! :D
Topic archived. No new replies allowed.