Need help with my Structs

My program allows a user to enter in player names and their stats and I need to use structs. I also put an array inside of my struct for the names and the stats. I have a lot of errors that Im not really sure were to begin to correct. Mainly they are dealing with my functions. I'm not sure if I'm writing my functions correctly when trying to input data into the stuct and output the data to the screen. Just some FYI I have the fstream header in becasue I plan on making the program read and right to a file as well. I'm trying to get this part fixed that way I can expand what the program needs to do. Also I check my code at codepad.org and it is showing an error when I call my function in main. Thank you for any help I get and again I will use your screen name to give credit for helping me.

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


using namespace std;

const int ARRAY_SIZE = 2 ; // declare my constant for array size to make it easier to change amount of players being entered

// struct to hold MLB player stats
struct playerStats
{
	string playerName[ARRAY_SIZE] ; // array containing player names
	int home_runs[ARRAY_SIZE] ;     // array containing number of home runs
	int hits[ARRAY_SIZE] ;	     // array containing number of hits
}tempStats;			     // variable of struct
 
// function prototypes
void intro()
int playerInfo(playerStats& tempStats) ;
void dataOutput(playerStats) ;



int main()
{
	
	intro() ;
	playerInfo(playerStats& tempStats) ;
	dataOutput(playerStats) ;

	
	system ("PAUSE") ;

	return 0 ;
 }


//functions

void intro()
{
	cout << " This program allows the user to enter in the stats of 10 MLB players." << endl << endl ;
}

int playerInfo(playerStats& tempStats)
{
	cout << "Enter name of Player: ";
	cin >> tempStats.playerName ;
	
	cout << "Enter the Player's number of homeruns: ";
	cin >> tempStats.home_runs ;
	
	cout << "Enter the Player's number of hits: ";
	cin >> tempStats.hits ;
	
	cout << endl ;
}

void dataOutput(playerStats)
{
	 for(int i=0 ; i < ARRAY_SIZE ; i++)
	 {
		 cout << playerStats.playerName[i] 
			  << " has "<< playerStats.home_runs[i] 
		      << " homeruns and " 
		      << playerStats.hits[i] << " hits" << endl ;
	 }
}
  
It would be helpful to see your errors, but just from looking at this code listing:

You need a semi-colon at the end of line 19.

On line 29, you are passing the address of your structure; however, that is unneeded because your playerInfo() function takes a reference (so just remove the ampersand on line 29.)

The playerInfo() function needs to be changed to use your arrays properly. Like in your dataOutput() function, you loop through each array element to display the stats -- you need to do a similar thing when inputting your stats.

I hope that gets you on your way.
@ kooth
when I call my functions in main I have errors saying type name is not allowed. For my playerInfo function I have erros on all my cins saying no operator ">>" matches these operands and I also get a nonstatic member reference must be relative to a specific object in my dataOutput function. I thought I had it typed correctly.
Ok here is my updated code but in line 28 and 29 I get type name not allowed and for some reason i get an error on line 65 that says expected a ";" I checked and all my semi-colons are good now.

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


using namespace std;

const int ARRAY_SIZE = 2 ; // declare my constant for array size to make it easier to change amount of players being entered

// struct to hold MLB player stats
struct playerStats
{
	string playerName[ARRAY_SIZE] ; // array containing player names
	int home_runs[ARRAY_SIZE] ;     // array containing number of home runs
	int hits[ARRAY_SIZE] ;			// array containing number of hits
}tempStats;							// variable of struct
 
// function prototypes
void intro() ;
int playerInfo(playerStats tempStats) ;
void dataOutput(playerStats) ;



int main()
{
	
	intro() ;
	playerInfo(playerStats tempStats) ;
	dataOutput(playerStats) ;

	
	system ("PAUSE") ;

	return 0 ;
 }


//functions

void intro()
{
	cout << " This program allows the user to enter in the stats of 10 MLB players." << endl << endl ;
}

int playerInfo()
{
	playerStats tempStats ;

	for(int i=0 ; i < ARRAY_SIZE ; i++)
	{

		cout << "Enter the player's name: ";
		cin >> tempStats.playerName[i] ;
	
		cout << "Enter the player's number of homeruns: ";
		cin >> tempStats.home_runs[i] ;
	
		cout << "Enter the player's number of hits: ";
		cin >> tempStats.hits[i] ;
	
		cout << endl ;
}

void dataOutput(playerStats)
{
	 for(int i=0 ; i < ARRAY_SIZE ; i++)
	 {
		 cout << playerStats.playerName[i] 
			  << " has "<< playerStats.home_runs[i] 
		      << " homeruns and " 
		      << playerStats.hits[i] << " hits" << endl ;
	 }
}
 
In line 29, take out the word "playerStats". You only need a variable when you call a function.

In line 30, you are passing the name of your structure, not the variable (change it to "tempStats").

Your "for" loop is missing a right-curly brace in playerInfo(), probably after line 62.
Thank you kooth I finally got to the point were I can compile after correcting other errors I had but you got me in the right direction. I will use your screen name to give you credit for helping.
Topic archived. No new replies allowed.