1D Arrays and 2D Arrays

Pages: 12
closed account (48T7M4Gy)
and for kemort, I ran that revised code and it wont enter in just 2 members, and when I do submit just the 2, it says enter number 0's name instead of number 1's. Should I just change that from being index = 0 to index = 1?


I fixed the code. There was a typo index++; was the problem.
closed account (48T7M4Gy)
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 <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

void getPlayerNames( string[], int&);
    
int main()
{
	const int MAX = 100;
	string name[MAX];
	int limit = 0;
	
	getPlayerNames( name, limit);

	for (int i = 0; i < limit; i++)
		cout << name[i] << endl;

	return 0;
}

void getPlayerNames( string aName[], int& aLimit)
{
    cout << "How many players? ";
    cin >> aLimit;
    
    for( int index = 0; index < aLimit; index++)
	{
		cout << "Player " << index << " name: ";
		cin >> aName[index];
	}
	return;
}


(This obviously doesn't have error checking to ensure aLimit < MAX)
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

struct Player
{
    string name;
    int AB;
    int Hits;
    double Ave;
};

void getPlayers( Player[], int&);
    
int main()
{
	const int MAX = 100;
	Player team[MAX];
	int limit = 0;
	
	getPlayers( team, limit);

	for (int i = 0; i < limit; i++)
	cout << team[i].name << ' ' << team[i].Hits << endl;

	return 0;
}

void getPlayers( Player aTeam[], int& aLimit)
{
    cout << "How many players? ";
    cin >> aLimit;
    
    for( int index = 0; index < aLimit; index++)
	{
		cout << "Player " << index << " name: ";
		cin >> aTeam[index].name;	
		
		cout << "Player " << index << " hits: ";
		cin >> aTeam[index].Hits;		
	}
	
	return;
}
You've helped me quite a bit Kemort! Thank you so much for your help! I have to turn in my assignment now unfortunately, unfinished but I appreciate you helping me with this! I actually kind of understand using the structure stuff now! Thank you again for the help!
closed account (48T7M4Gy)
Cheers :)
Topic archived. No new replies allowed.
Pages: 12