error from ifstream string into class.

Write your question here.
I am taking computer science course in uni, and this is my first year, and
my prof gave us assignment to simulate soccer program, and at the starting stage,
I am trying to make team data with txt file and trying to ifstream into the program and put it as a class/structure. And I am messed up now.

Here's my code that I messed up.


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//main.cpp
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include "title.h"
#include "startscreen.h"
using namespace std;

int main()
{
	title();  //shows title screen
	startscreen();
}

//startscreen.h
#pragma once
#include <iostream>
#include <conio.h>
#include "team.h"
#include "teameditP.h"
#include "teameditT.h"
using namespace std;

void startscreen()
{
	while (1)
	{
		auto_team();
		cout << "=*=*=*Welcome to FIFA World Cup 2018!=*=*=*\n\n\n";
		cout << "\t1.Just Start with team without editing.\n\n";
		cout << "\t2.Edit the team data for this run only.\n\n
		cout << "\t3.Edit the team data peermanently." << endl << endl;
		cout << "Press the option number!";
		char option = getch();
		if (option == '1') {
			system("cls");
			for (int i = 0; i < 4;i++)	//temporarilly 4(or temporary printing)
				printteam(Team[i]);
			return;
		}

//team.h
#pragma once
#include <iostream>
#include <fstream>
#include "player.h"
using namespace std;

class team
{
public:
	friend void auto_team();
	void getname(string nn)
	{
		team_name = nn;
	}
	void gethome(bool tf)
	{
		home = tf;
	}
	void getplayer(int k, int a, int b, string nn, int c, double d, int e, int f, bool g)
	{
		players[k].num = a;
		players[k].position = b;
		players[k].name = nn;
		players[k].age = c;
		players[k].condition = d;
		players[k].contribution = e;
		players[k].yellow = f;
		players[k].avail = g;
	}
	void gettraining(double a)
	{
		training = a;
	}
	void getevent(double e)
	{
		event = e;
	}
	void getattack(int a)
	{
		attack = a;
	}
	void getdefense(int a)
	{
		defense = a;
	}
	void getstats(int a)
	{
		stats = a;
	}
	friend void printteam(team a);
	void edit();
private:
	string team_name;
	bool home;
	player players[19];
	double training;	//inital value 1.0
	double event;		//inital value 1.0
	int attack;
	int defense;
	int stats;	    //reference fifa 2016 (mobile: cuz its free)
};

//team.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "team.h"
using namespace std;

void auto_team()
{
	team Team[4];		//temporarilly 4, change to 32
	ifstream fin;
	ofstream fout;
	fin.open("DATA.txt");
	int a, b, c, d, e;
	string nn;
	double dd;
	bool f;
	
	for (int i = 0; i < 4; i++)		//change to 32
	{
		fin.getline >> nn;
		Team[i].getname(nn);
		fin >> f;
		Team[i].gethome(f);
		for (int i = 0; i < 19; i++)
		{
			fin >> a >> b;
			fin.getline >> nn;
			fin >> c >> dd >> d >> e >> f;
			Team[i].getplayer(i, a, b, nn, c, dd, d, e, f);
		}
		fin >> dd;
		Team[i].gettraining(dd);
		fin >> dd;
		Team[i].getevent(dd);
		fin >> a;
		Team[i].getattack(a);
		fin >> a;
		Team[i].getdefense(a);
		fin >> a;
		Team[i].getstats(a);
	}
}

void printteam(team a)
{
	cout << "Team Name: ";
	cout << a.team_name << " ";
	if (a.home)
	{
		cout << "(Hometeam)";
	}
	cout << endl;
	cout << "**************   Players   **************" << endl;
	for (int i = 0; i < 19; i++)
	{
		cout << a.players[i].num << ". " << a.players[i].name << " (";
		cout << a.players[i].age << ")\t";
		if (a.players[i].position == 0)
			cout << "GK ";
		else if (a.players[i].position == 1)
			cout << "DF";
		else if (a.players[i].position == 2)
			cout << "MF";
		else
			cout << "FW";
		cout << "\t";
		cout << "G/A/S: " << a.players[i].contribution << endl;
	}
	cout << endl;
	cout << "<Strategy>" << endl;
	cout << "A:" << (a.attack * 10) << "%\tD: " << (a.defense * 10) << "%";
	cout << endl;
	cout << "Team Stats: " << a.stats << "/100";
}

//player.h
#pragma once
#include <iostream>
using namespace std;

struct player
{
	int num;
	int position;	//0:GK 1:DF 2.MF 3:FW
	string name;
	int age;
	double condition;	//4 
	int contribution;	//	
	int yellow;		
	bool avail;
};
 


and here's my data file

Germany
0
12 0
Ron-Robert_Zieler
26 1.0 0 0 0
22 0
Bernd_Leno
23 1.0 0 0 0
24 0
Kevin_Trapp
25 1.0 0 0 0
2 1
Shkodran_Mustafi
23 1.0 0 0 0
4 1
Sebastian_Rudy
28 1.0 0 0 0
5 1
Mats_Hummels
22 1.0 0 0 0
14 1
Emre_Can
25 1.0 0 0 0
16 1
Antonio_Rudiger
22 1.0 0 0 0
18 1
Matthias_Ginter
21 1.0 0 0 0
6 2
Sami_Khedira
28 1.0 0 0 0
8 2
Julian_Draxler
22 1.0 0 0 0
9 2
Andre_Schurrle
25 1.0 0 0 0
11 2
Karim_Bellarabi
25 1.0 0 0 0
13 2
Thomas_Muller
26 1.0 0 0 0
20 2
Christoph_Kramer
24 1.0 0 0 0
21 2
Ilkay_Gundogan
25 1.0 0 0 0
15 3
Kevin_Volland
23 1.0 0 0 0
19 3
Max_Kruse
27 1.0 0 0 0
23 3
Mario_Gomez
30 1.0 0 0 0
1.0
1.0
5
5
90

Argentina
0
1 0
Sergio_Romero
28 1.0 0 0 0
12 0
Nahuel_Guzman
29 1.0 0 0 0
23 0
Agustin_Marchesin
27 1.0 0 0 0
15 1
Martin_Demichelis
34 1.0 0 0 0
16 1
Marcus_Rojo
25 1.0 0 0 0
17 1
Nicolas_Otamendi
27 1.0 0 0 0
13 1
Ramiro_Funes_Mori
24 1.0 0 0 0
4 1
Gino_Peruzzi
23 1.0 0 0 0
21 1
Emmanuel_Mas 26 1.0 0 0 0
14 2
Javier_Mascherano 31 1.0 0 0 0
10 2
Angel_Di_Maria
27 1.0 0 0 0
19 2
Ever_Banega
27 1.0 0 0 0
6 2
Lucas_Biglia
29 1.0 0 0 0
8 2
Enzo_Perez
29 1.0 0 0 0
11 2
Erik_Lamela
23 1.0 0 0 0
20 2
Nicolas_Gaitan
27 1.0 0 0 0
9 3
Gonzalo_Higuain
27 1.0 0 0 0
22 3
Ezequiel_Lavezzi
30 1.0 0 0 0
18 3
Angel_Correa
20 1.0 0 0 0
1.0
1.0
5
5
90

Netherlands
0
22 0
Maarten_Stekelenburg
33 1.0 0 0 0
1 0
Jasper_Cillessen
26 1.0 0 0 0
23 0
Jeroen_Zoet
24 1.0 0 0 0
8 1
Daley_Blind
25 1.0 0 0 0
2 1
Daryl_Janmaat
26 1.0 0 0 0
15 1
Erik Pieters
27 1.0 0 0 0
3 1
Jeffrey_Bruma
24 1.0 0 0 0
13 1
Joel_Veltman
23 1.0 0 0 0
5 1
Terence_Kongolo
21 1.0 0 0 0
10 2
Wesley_Sneijder
31 1.0 0 0 0
17 2
Georginio_Wijnaldum
25 1.0 0 0 0
6 2
Jordy_Clasie
24 1.0 0 0 0
16 2
Marko_Vejinovic
25 1.0 0 0 0
19 3
Klaas_Jan_Huntelaar
32 1.0 0 0 0
21 3
Memphis_Depay
21 1.0 0 0 0
14 3
Luuk_de_Jong
25 1.0 0 0 0
7 3
Quincy_Promes
23 1.0 0 0 0
9 3
Bast_Dost
26 1.0 0 0 0
1.0
1.0
5
5
90

Brazil
0
1 0
Jefferson
32 1.0 0 0 0
23 0
Alisson
23 1.0 0 0 0
12 0
Cassio
28 1.0 0 0 0
15 1
Dani_Alves
32 1.0 0 0 0
3 1
Miranda
31 1.0 0 0 0
16 1
Filipe_Luis
30 1.0 0 0 0
2 1
Danilo 24
1.0 0 0 0
14 1
Gil
28 1.0 0 0 0
13 1
Gabriel_Paulista
25 1.0 0 0 0
22 2
Kaka
33 1.0 0 0 0
11 2
Oscar
24 1.0 0 0 0
17 2
Luiz_Gustavo
28 1.0 0 0 0
19 2
Willian
27 1.0 0 0 0
8 2
Elias
30 1.0 0 0 0
5 2
Fernandinho
30 1.0 0 0 0
7 2
Douglas_Costa
25 1.0 0 0 0
10 3
Neymar
23 1.0 0 0 0
21 3
Hulk
29 1.0 0 0 0
9 3
Ricardo_Oliveira
35 1.0 0 0 0
1.0
1.0
5
5
90


That's a lot of code you posted, which may or may not be a good thing. It all depends on what is meant by "I messed up". Is there some error message either during compiling or running which might clarify what is the problem?

One thing I would say, it's not good practice to put "using namespace std;" in a header file. It means that any other file which includes that header directly or indirectly will find the entire contents of the std namespace dumped into it. Better to explicitly type the qualifier such as std::string where required.
After a very quick glance you may want to insure that your braces, brackets, parentheses, and quotation marks all have matches.
closed account (48T7M4Gy)
I am trying to make team data with txt file and trying to ifstream into the program and put it as a class/structure. And I am messed up now.

You've fallen into the trap a lot of people find themselves in having written huge amounts of code then striking a problem and keep going with trying to find a needle in a haystack.

Most of your program is menus, voluminous class methods and other extraneous stuff.

I suggest you concentrate on writing a small program and read in the data and print it out. That is about the first task you should have carried instead of leaving it to now.

Once you've got that going go back to the stuff you currently have and proceed from there.

Your test program and cut down class will take about 30 minutes to write and be approximately 20 lines - trust me.

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Topic archived. No new replies allowed.