Program crash

Am making my own small program to analyse football games. i am reading a text file and using three funtions to analyse the results.

The program is crashing on reaching ss>> obj.Field1>>space; below. What could be the problem ?
Enter the word "Arezzo" when testing this snippet. ----The text file is as shown below the code.
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
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <cstdlib>
using namespace std;


class teams  {
public:
std::string home;   //will hold user input
char chr1[8], chr2 [8], chr3 [8];     //variables to store date characters
char Field1 [15];  //will hold team name
	string Field4;   //will hold team name
int Field2, Field3;  //will hold goals
std::vector<teams>myvec;


void AnalyseGames (std::vector<teams> &myvec );

};

void AnalyseGames ( std::vector<teams> &myvec);


int main () {

	 std::vector<teams> myvec;
teams mine;

mine.AnalyseGames (myvec);


return 0;
}

void teams::AnalyseGames (std::vector<teams> &myvec )

{
	teams obj;

std::string line ="";    //variable will hold lines temporarily in memory
char space;      //varable will hold either a small space or full colon ":"

std::cout<<"Enter the name of the team you want to obtain its analysis\n"<<endl;
getline (cin, obj.home);
std::cout<<endl;

ifstream inputfile ("myFile.txt");   //opening file in read mode
if (!inputfile) {   "File failed to open !"; std::system ("pause"); exit (1);    }

while (std::getline (inputfile, line));   //parsing strings 
     {

istringstream ss(line);  //separating the fields
if (!ss)  {   std::cout<<"Stringstream failure 1 !\n"; system ("pause"); exit (1);    }

//01st May 16		Arezzo	1:0	Maceratese  ----format of each line

ss>> obj.chr1;      //01st 

std::cout<<obj.chr1<<" chr1\t";
if (!ss)  {    std::cout<<"Stringstream failure on chr1 !\n"; system ("pause"); exit (1);     }

ss>> obj.chr2;        //May
if (!ss)  {   std::cout<<"Stringstream failure on chr2 !\n"; system ("pause"); exit (1);    }
std::cout<<obj.chr2<<" chr2\t";

ss>> obj.chr3;       // 16
if (!ss)  {    std::cout<<"Stringstream failure on chr3 !\n"; system ("pause"); exit (1);   }
std::cout<<obj.chr3<<" chr3\t";

//ss.clear ();
ss>> obj.Field1>>space;     //Arezzo           -------problem here

if (!ss)  {   std::cout<<"Stringstream failure field1 !\n"; system ("pause"); exit (1);    }
std::cout<<obj.Field1<<" Field1\t";


 ss>>obj.Field2>>space;  //first goal field and space is character ":"  e.g goal 1 above
 if (!ss)  {std::cout<<"Stringstream failure 2 !\n"; system ("pause"); exit (1);}
 std::cout<<obj.Field2<<" Field2\t";


ss>> obj.Field3;    //second goal field  e.g. goal 0 above
 std::cout<<obj.Field3<<" Field3\t";
if (!ss)  {  std::cout<<"Stringstream failure 3 !\n"; system ("pause"); exit (1);   }




if (!ss)  {  std::cout<<"Stringstream failure 4 !\n"; system ("pause"); exit (1);  }
getline (ss, obj.Field4); //Arezzo //Macerrate 
 std::cout<<obj.Field4<<" Field4\t";

if (!ss)  {   std::cout<<"Stringstream failure 5 !\n"; system ("pause"); exit (1);  }



if (ss) {	myvec.push_back (obj);}
if (!ss)  {   std::cout<<"Stringstream failure 6 !\n"; system ("pause"); exit (1);       }

    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
28th Nov 15		Arezzo	1:1	Lupa Roma
22nd Nov 15		Robur Siena	1:1	Arezzo
15th Nov 15		Arezzo	1:2	Lucchese
11th Nov 15		Savona	0:0	Arezzo
08th Nov 15		Teramo	1:1	Arezzo
31st Oct 15		Arezzo	0:0	US Ancona 1905
24th Oct 15		Pontedera	1:1	Arezzo
17th Oct 15		Arezzo	1:0	Prato
10th Oct 15		Arezzo	1:4	Carrarese
04th Oct 15		Savona	-:-	Arezzo
04th Oct 15		Torres	-:-	Arezzo
26th Sep 15		Arezzo	1:1	Pisa
19th Sep 15		Tuttocuoio	0:1	Arezzo
13th Sep 15		Arezzo	0:1	L'Aquila
07th Sep 15		Santarcangelo	1:1	Arezzo 
Remove the semicolon on line 55.

The >> operator automatically skips all spaces so you should not use the variable space. The way you use it in the code you're actually reading the '1' after "Arezzo", the space is simply ignored.

1
2
3
ss >> obj.Field1 >> space;
cout << obj.Field1 << endl; // "Arezzo"
cout << space << endl; // "1" 
Thanks Peter, that correction did it.

Am reading the file into the class variables.
However, i cannot use the strings in place of the char array since i noted that when i read strings using getline function, it takes a longer field eating into the integer fields (i used the space delimiter ' ' but wouldnt do).

So i used the char arrays and am reading the fields fine into a vector.

I would like to sort and compare two vectors of the class type "teams" and therefore needs a constructor to the class below.

What is the problem with the constructor when assigning the character arrays to the constructor pointers ?

Problem is here: chr1 (date), chr2(month) , chr3 (year), Field1 (HomeName)
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
using namespace std;


class teams  {
	private:

char chr1 [8] , chr2 [4] , chr3 [4] ;     //variables to store date characters
char Field1 [15];  //holds first team name
char space;
	string Field4; //holds second team name
int Field2, Field3;    //holds number of goals

public:

teams (char *date ,char *month,char *year,char *HomeName, int &HomeGoal, char  &colon, int &AwayGoal,  string &AwayName ); 
teams (){}

//std::string home;
	char home [12];

};


teams::teams (char *date  ,char *month ,char *year ,char *HomeName  , int &HomeGoal, char  &colon, int &AwayGoal,  string &AwayName ): 
chr1  (date), chr2(month) , chr3 (year),  Field1 (HomeName),  Field2 (HomeGoal), space (colon), Field3 (AwayGoal),   Field4 (AwayName) 
{}


Error i get is a value of type "char *" cannot be used to initialize an entity of type "char [8]"
Last edited on
Topic archived. No new replies allowed.