Errors

ok so I have this assignment for class, and im trying to run it, but everytime i try to compile it, it tells me that it "cannot convert 'std::string' to 'std::string*' for argument 1" to both buildAr and printAr functions. what am i doing wrong?

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


const int MAX = 35;
int goals[MAX];
int assists[MAX];
std::string playerNames[MAX];
int buildAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX]);
void printAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX], int numPlayers);
void sortAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX], int numPlayers );

using namespace std;

int main()
{
int numPlayers;
numPlayers = buildAr(playerNames[MAX], goals[MAX], numPlayers);
printAr(playerNames[MAX], goals[MAX], assists[MAX], numPlayers);

return 0;
}






///////////////////////////////////////////////////////////

void printAr (std::string playerNames[MAX], int goals[MAX], int assists[MAX], int numPlayers)
{
int i = 0;
int pnts;
pnts= goals[i] + assists[i];

	while((i-1)<=numPlayers)
	{
	cout<<playerNames[i]<<setw(6)<<goals[i]<<setw(6)<<assists[i]<<setw(6)<<pnts;
	i++;
	}
	
}




/////////////////////////////////////////////////////////////////
int buildAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX])
{

int gls;
int ats;
std::string playersName;

std::ifstream statFile;

statFile.open("C:\\Users\\DREWTOP\\Desktop\\School Work\\CSCI\\hockey.txt");

int sub1=0;

if ( statFile.fail() )     
  {
  cout << "open for hockey.txt failed";
  exit(-1);
  }

	statFile>>playersName;

	while(statFile)
	{
	statFile>>gls;
	statFile>>ats;
	playerNames[sub1]=playersName;
	goals[sub1]=gls;
	assists[sub1]=ats;

	cout<<playerNames[sub1]<<endl;
	statFile>>playersName;
	sub1+=1;
	}
	
	cout<<sub1;
	
  statFile.close();
  
 
  return sub1 + 1;
}

You don't need to pass the size of the first dimension of an array in the argument to a function. You should just pass your arrays like this "ARRAY[]".

EDIT: It would just be easier to have a for loop in your main function iterate through the array's and pass the elements as individual variables.
Last edited on
i have no idea what you are saying really. Where do I have to leave out the MAX? are you sure this is why I am getting these errors?
See Here: http://www.cplusplus.com/doc/tutorial/arrays/
Search for "Arrays as parameters".

Right now you're not passing the array to your function you're passing a pointer to the last element in the array which is why you're getting that error.
okay so i did that, however now its telling me that i have an invalid conversion of 'int' to 'int*' for line 19
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
#include <iostream>
#include <iomanip>
#include <fstream>


const int MAX = 35;
int goals[MAX];
int assists[MAX];
std::string playerNames[MAX];
int buildAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX]);
void printAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX], int numPlayers);
void sortAr(std::string playerNames[MAX], int goals[MAX], int assists[MAX], int numPlayers );

using namespace std;

int main()
{
int numPlayers;
numPlayers = buildAr(playerNames, goals, numPlayers);
printAr(playerNames, goals, assists, numPlayers);

return 0;
}






///////////////////////////////////////////////////////////

void printAr (std::string playerNames[MAX], int goals[MAX], int assists[MAX], int numPlayers)
{
int i = 0;
int pnts;
pnts= goals[i] + assists[i];

	while((i-1)<=numPlayers)
	{
	cout<<playerNames[i]<<setw(6)<<goals[i]<<setw(6)<<assists[i]<<setw(6)<<pnts;
	i++;
	}
	
}




/////////////////////////////////////////////////////////////////
int buildAr(std::string playerNames[], int goals[], int assists[])
{

int gls;
int ats;
std::string playersName;

std::ifstream statFile;

statFile.open("C:\\Users\\DREWTOP\\Desktop\\School Work\\CSCI\\hockey.txt");

int sub1=0;

if ( statFile.fail() )     
  {
  cout << "open for hockey.txt failed";
  exit(-1);
  }

	statFile>>playersName;

	while(statFile)
	{
	statFile>>gls;
	statFile>>ats;
	playerNames[sub1]=playersName;
	goals[sub1]=gls;
	assists[sub1]=ats;

	cout<<playerNames[sub1]<<endl;
	statFile>>playersName;
	sub1+=1;
	}
	
	cout<<sub1;
	
  statFile.close();
  
 
  return sub1;
}


You're trying to pass "numPlayers", an integer, to buildAr where it expects to see an array in the third argument.
wow, thank you. lol i woulda been stuck all night
Topic archived. No new replies allowed.