Baseball Roster Help?

Hey all, I'm trying to make a program to have a baseball roster which can be edited and adjusted based on user input. Everything works 100% fine except for one small problem... I can't seem to figure out how to have my program add just one player to the roster on top of everything else without editing the entire roster itself. Any tips on how to fix this? Specifically in my function "addPlayer".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void addPlayer (data player[SIZE]){
for (int i = 0; i < SIZE; i++){
if (player[i].name == ""){
    break;
}
else {
    cout << "Please enter in the name for player " << i+1 << ": ";
    cin >> player[i].name;
    cout << "Enter player " << i+1 << "'s number of hits: ";
    cin >> player[i].hits;
    cout << "Enter player " << i+1 << "'s number of homeruns: ";
    cin >> player[i].homeruns;

while (player[i].hits < player[i].homeruns){
    cout << "You can't have more hits than homeruns..." << endl << "Please enter a proper amount of homeruns! \(Less than or equal to "
    << player[i].hits << "\): ";
    cin >> player[i].homeruns;


            }
        }
    }
}



For clarification on what I'm doing here, I was trying to make it so that if the player at index position [i] was full of another string, it would skip that player and move on to the next index position, repeated until it found empty index position. I know what I did wasn't the right way to do it AT ALL, but I'm looking for some help on what the proper way would be.

Last edited on
Does this work any better?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void addPlayer (data player[SIZE]){
int i = 0;// so scope is function wide

for ( i = 0; i < SIZE; i++)
if (player[i].name == "") break;

if( i < SIZE ) {
    cout << "Please enter in the name for player " << i+1 << ": ";
    cin >> player[i].name;
    cout << "Enter player " << i+1 << "'s number of hits: ";
    cin >> player[i].hits;
    cout << "Enter player " << i+1 << "'s number of homeruns: ";
    cin >> player[i].homeruns;

    while (player[i].hits < player[i].homeruns){
        cout << "You can't have more hits than homeruns..." << endl << "Please enter a proper amount of homeruns! \(Less than or equal to "
    << player[i].hits << "\): ";
        cin >> player[i].homeruns;
}
    else    
          cout << "All " << SIZE << " spaces are taken already" << endl;    
}
After inputing player 1's data it seems to still want to edit player 1 instead of adding player 2. :/
Ah, fixed it. Just some stuff needed to be moved around. Thanks.
Topic archived. No new replies allowed.