#include <iostream>
using namespace std;
struct person
{
char name[15];
int *scores;
};
//// function prototype //////////
void allocate_arrays(int number_players,int number_scores);
//////////////////////////////////
int main()
{
int number_scores;
int number_players;
cout << "How many players in your team?: ";
cin >> number_players;
cout << endl;
cout << "How many scores each player will have?: ";
cin >> number_scores;
cout << endl;
/* pass the number of players and how many scores to memry allocation function: */
allocate_arrays (number_players , number_scores);
return 0;
} // end of main
///////////// functions defintions //////////////
void allocate_arrays(int number_players , int number_scores)
{
person *player;
player = new person[number_players];
int *scores; // declare an int pointer
scores[number_players] = new int[number_scores];
/* the above statement gives error "can not convert from int* to int " using the last statement above instead of scores = new int [number_scores] */
return ;
} // end of allocate arrays function
to get the total scores for each player is easy by using a for-loops.
my questions are:
1)how to allocate memory for x number of scores for each player? in this format :
int *scores; // declare an int pointer
scores[number_players] = new int[number_scores];
//for each player, he has number of scores array.
2) how to pass the scores pointer from main to the function? do I have to declare the pointer in the function.
3) scores pointer declared in the structure was it global to all functions?
using namespace std;
struct person
{
char name[15];
int *scores;
};
//// function prototype //////////
void allocate_arrays(int number_players,int number_scores);
//////////////////////////////////
int main()
{
int number_scores;
int number_players;
cout << "How many players in your team?: ";
cin >> number_players;
cout << endl;
cout << "How many scores each player will have?: ";
cin >> number_scores;
cout << endl;
/* pass the number of players and how many scores to memry allocation function: */
allocate_arrays (number_players , number_scores);
return 0;
} // end of main
///////////// functions defintions //////////////
void allocate_arrays(int number_players , int number_scores)
{
person *player;
player = new person[number_players];
int *scores; // declare an int pointer
scores[number_players] = new int[number_scores];
/* the above statement gives error "can not convert from int* to int " using the last statement above instead of scores = new int [number_scores] */
return ;
} // end of allocate arrays function
to get the total scores for each player is easy by using a for-loops.
my questions are:
1)how to allocate memory for x number of scores for each player? in this format :
int *scores; // declare an int pointer
scores[number_players] = new int[number_scores];
//for each player, he has number of scores array.
2) how to pass the scores pointer from main to the function? do I have to declare the pointer in the function.
3) scores pointer declared in the structure was it global to all functions?