Help with passing values

Writing a program for class and I'm having a very hard time trying to pass values.

The assignment says "Write the procedure GetPlayerNames, which will accept two names as input from the users, and returns both names to the calling program.
For the two names in the main function, declare the names like this:
char player1[NAME_SIZE];
char player2[NAME_SIZE];"

This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void GetPlayerNames (char* x, char* y)
{
	cout << "Enter first name: ";
	cin.getline(x, NAME_SIZE);
	cout << "Enter second name: ";
	cin.getline (y, NAME_SIZE);
}

int main ()
{
	const int NAME_SIZE = 25;

	char player1;
	char player2;

	GetPlayerNames(&player1, &player2);

	cout << player1 << endl << player2;

	return 0;
}


If I enter "Trial" for the first name, and "Run" for the second name all that comes out is "u" and then it crashes.

I feel like I have to be missing something fundamental here and it's driving me crazy.
In your own words,
For the two names in the main function, declare the names like this:
char player1[NAME_SIZE];
char player2[NAME_SIZE];


and change the function call to
GetPlayerNames(player1, player2);
Also, function GetPlayerNames does not have access to the constant NAME_SIZE which is defined in main(), therefore the code will not compile. I suggest moving that constant out of main() and place it at the top, in the global scope.
The reason you have to do what Chervil say is because char is only one character, so you need to make an array of as many characters as you will need.
I'm still getting the same error taking out the & and moving the const out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int NAME_SIZE = 25;

void GetPlayerNames (char*x, char*y)
{
	cout << "Enter first name: ";
	cin.getline (x, NAME_SIZE);
	cout << "Enter second name: ";
	cin.getline (y, NAME_SIZE);
}

int main ()
{
	char player1;
	char player2;

	GetPlayerNames(player1, player2);

	cout << player1 << endl << player2;

	return 0;
}
You made the second (and third) change I suggested. But what about the first?

Look at lines 13 and 14 of your program:
13
14
    char player1;
    char player2;

Now look at this:
13
14
    char player1[NAME_SIZE];
    char player2[NAME_SIZE];

Do you see the difference?

The second version is the one you have been instructed to use - as you stated in the original post, and I also reminded you.

char player1; this can hold a single character such as 'A' or 'x'

char player1[NAME_SIZE]; this is an array of characters. It can contain a string of characters such as "John" or "The cat sat on the mat".

Last edited on
Topic archived. No new replies allowed.