Structures not working

Hi, I am working with structures for the first time. I am not able to cout the data I read from a file using a struct. My program is below. What am I doing wrong?

I appreciate your 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int NAM_LEN = 40;
const int TEAM_LEN = 10;
const int POS_LEN = 10;

struct player_info
{
	char playerName[NAM_LEN];
	char team[TEAM_LEN];
	char position[POS_LEN];
	char ownerName[NAM_LEN];
};



void main()
{	
	player_info player;

	ifstream inFile;
	inFile.open("fantasy.dat");

	


	cout<<"Player Name\t\tTeam\t\tPosition\t\tOwner Name"<<endl;
	cout<<"-----------\t\t-----\t\t-------\t\t\t----------"<<endl;

	int i = 0;

	while(!inFile.eof())
	{	
		inFile.getline(player.playerName, NAM_LEN);
		inFile.getline(player.team, TEAM_LEN);
		inFile.getline(player.position, POS_LEN);
		inFile.getline(player.ownerName, NAM_LEN);
		
		i++;
		}

	
	for(int j=0; j < i; j++)
	{	
	
		cout<<player.playerName[j]<<"\t\t"<<player.team[j]<<"\t\t"<<player.position[j]<<"\t\t"<<player.ownerName[j]<<endl;
	
	}


	inFile.close();
	
	system("pause");

}
closed account (iAk3T05o)
NEVER use void main(), not even mistakenly. main() is always int main().
Where did you even learn void main() from?
I learned it in class. I asked the prof about it and he said you can use void main when you want to be lazy and not write return 0;
Ok I edited my code with int main and I changed player_info player; to player_info player[MAX_PLAYERS];

I realized I have to declare an array variable for the struct. I change this throughout the code, but now I am receiving the following error message: Unhandled exception at 0x001b1102 in Assign 5.exe: 0xC0000005: Access violation writing location 0x000000aa.

My new code is below.

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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int NAM_LEN = 40;
const int TEAM_LEN = 10;
const int POS_LEN = 10;

struct player_info
{
	char playerName[NAM_LEN];
	char team[TEAM_LEN];
	char position[POS_LEN];
	char ownerName[NAM_LEN];
};



int main()	
{	
	const int MAX_PLAYERS = 20;
	player_info player[MAX_PLAYERS];

	ifstream inFile;
	inFile.open("fantasy.dat");

	cout<<"Player Name\t\tTeam\t\tPosition\t\tOwner Name"<<endl;
	cout<<"-----------\t\t-----\t\t-------\t\t\t----------"<<endl;

	int i = 0;

	while(!inFile.eof())
	{	
		inFile.getline(player[i].playerName, NAM_LEN);
		inFile.getline(player[i].team, TEAM_LEN);
		inFile.getline(player[i].position, POS_LEN);
		inFile.getline(player[i].ownerName, NAM_LEN);
		
		i++;
		}

	
	for(int j=0; j < i; j++)
	{	
	
		cout<<player[j].playerName<<"\t\t"<<player[j].team<<"\t\t"<<player[j].position<<"\t\t"<<player[j].ownerName<<endl;
	
	}


	inFile.close();
	
	system("pause");

	return 0;

}
I'd recommend using a debugger to find out exactly where this crashes, and exactly what the state of the memory is when it crashes.

Are you certain that i can never exceed MAX_PLAYERS?
i cannot exceed MAX_PLAYERS because there are only 20 lines in the data file. I increased MAX_PLAYERS from 20 to 50 but it still does not work. I can easily debug when I have an error list. However, there is not error list. I just get a pop-up message.
Do you understand what a debugger is, and what it does? If not, I strongly recommend you find a tutorial for whichever debugger comes with your IDE, and learn how to use it. You;ll find it invaluable in finding and fixing this sort of problem.
What does the file fantasy.dat contain? Perhaps you could post the contents of the file that you are trying to read.

Note: The run-time error is because an attempted input failed and the member arrays are uninitialized.
Ok I will look into a tutorial on the debugger. I am using Microsoft Visual C++ 2010 Express as my IDE. I know you can do breakpoint and that it would help you identify the error, but all the code from the debugger is mystifying to me at this point.

The data file contains the following:

ANDERSON SF K JOANN
TIMPSON PHIL WR JIM
ELWAY DEN QB JACKV
SALAAM CHI RB BILL
CARNEY SD K VINCE
SANDERS DET RB JOHN
CARTER MINN WR JACKR
FREEMAN GB WR RON
WATTERS PHIL RB CHRIS
CHMURA GB TE STEVE
BLEDSOE NE QB CHRIS
FAULK IND RB JIM
GEORGE TENN RB STEVE
BETTIS PITT RB JACKV
STEWART PITT QB BILL
DUDLEY OAK TE JOHN
MARINO MIA QB VINCE
KIRBY SF RB RON
WARREN SEA RB JACK
MARTIN NE RB JOANN
inFile.getline() tries to read a complete line.

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
#include <iostream>
#include <string>
#include <fstream>

#include <vector> // *** added

using namespace std;

// const int NAM_LEN = 40;
// const int TEAM_LEN = 10;
// const int POS_LEN = 10;

struct player_info
{
        // see: http://www.mochima.com/tutorials/strings.html
	string playerName ;
	string team ;
	string position ;
	string ownerName ;
};



int main()
{
	// const int MAX_PLAYERS = 20;
	// player_info player[MAX_PLAYERS];

	// see: http://www.mochima.com/tutorials/vectors.html
	vector< player_info > player ;

	// ifstream inFile;
	// inFile.open("fantasy.dat");
	ifstream inFile( "fantasy.dat" ) ; // the construction will open it

	cout<<"Player Name\t\tTeam\t\tPosition\t\tOwner Name"<<endl;
	cout<<"-----------\t\t-----\t\t-------\t\t\t----------"<<endl;

	// int i = 0;

	player_info temp ;

	while( inFile >> temp.playerName >> temp.team >> temp.position >> temp.ownerName )
	    player.push_back(temp) ;

	for( size_t j = 0 ; j < player.size() ; ++j )
	{
		cout<<player[j].playerName<<"\t\t"<<player[j].team<<"\t\t"
		    <<player[j].position<<"\t\t"<<player[j].ownerName<<endl;
	}


	// inFile.close(); and the destructor will close it

	// system("pause");

	// return 0;

}



Small observation regarding this:

magyar wrote:
I learned it in class. I asked the prof about it and he said you can use void main when you want to be lazy and not write return 0;


There is a special rule for the main() function: return 0; is implied if the return statement is missing.

1
2
3
4
5
int main()
{
    // no errors and no warnings for missing:
    // return 0;
}


http://www.stroustrup.com/bs_faq2.html#void-main
Last edited on
Thanks, I understand the vectors but my professor doesn't want us to go beyond what he's taught. Is there anyway to do this with chars or just strings?
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>
using namespace std;

struct player_info
{
    string playerName ;
    string team ;
    string position ;
    string ownerName ;
};

int main()
{
	const int MAX_PLAYERS = 20; // maximum possible size
	player_info player[MAX_PLAYERS]; // array
	int num_players = 0 ; // actual size in use

	ifstream inFile( "fantasy.dat" ) ; // the construction will open it

	cout<<"Player Name\t\tTeam\t\tPosition\t\tOwner Name"<<endl;
	cout<<"-----------\t\t-----\t\t-------\t\t\t----------"<<endl;


	player_info temp ;
	while( inFile >> temp.playerName >> temp.team >> temp.position >> temp.ownerName &&
               num_players < MAX_PLAYERS ) // not more than MAX_PLAYERS times
        {
            player[num_players] = temp ;
            ++num_players ;
        }

	for( int j = 0 ; j < num_players ; ++j )
	{
		cout<<player[j].playerName<<"\t\t"<<player[j].team<<"\t\t"
		    <<player[j].position<<"\t\t"<<player[j].ownerName<<endl;
	}
}
Last edited on
Topic archived. No new replies allowed.