for some reason I cannot add integers in an array of structures. here is the code.

#include<iostream>
#include<iomanip>
using namespace std;

const int SIZE = 5;

struct playerInfo {
    char name[30];  //Players name
    int number;
    int points;
};

int main()
{
    int total = 0;   // to hold total points scored
	
    playerInfo player[SIZE];
    
    cout << "Enter the information for five players:\n\n";
    
    for (int i = 0; i < SIZE; i++)
    {
    	cout << "Player " << i+1 << " info\n";
        cout << "Enter the name of the player :";
        cin >> player[i].name;
        cout << "Enter the players number :";
        cin >> player[i].number;
        cout << "Enter the players points :";
        cin >> player[i].points;
        cout << "------------------------------\n";
    }
    
    //calculate total
    for (int i = 0; 0 < SIZE; i++)
    {
    	total = (player[i].points + total);
    }

    //showResult
    
    cout << "Here is a list of players and thier scores\n";
    cout << "------------------------------------------\n";
    for (int i = 0; i < SIZE; i++)
    {
        cout << player[i].name << "\tPoints:" << player[i].number << "\tScore:" << player[i].points << endl;
    }

    cout << "The total points scord by the team is :" << total << endl;
    
return 0;
}

If I comment this out

//calculate total
    for (int i = 0; 0 < SIZE; i++)
    {
    	total = (player[i].points + total);
    }

the program will run fine without giving me the total obviosly. This code crashes the program...

I also tried

total += player[i].points

with no luck.

any help would be great!
Cheers

you have to flush the input keyboard buffer of the '\n' after each numeric input

cout << "Enter the players number :";
        cin >> player[i].number;
        cin.ignore();
        cout << "Enter the players points :";
        cin >> player[i].points;
        cin.ignore();

line 34: that's an infinite loop because 0 is ALWAYS less than SIZE ! Correction here: for (int i = 0; i < SIZE; i++)

Sometimes it is the simplest things...

Thanks for your help!

I got my loop fixed. I changed the input a bit to fix the keyboard buffer, how does this look?

cout << "Player " << i+1 << " info\n";
        cout << "Enter the name of the player :";
        cin.getline(player[i].name, NAME_LENGTH);
        cout << "Enter the players number :";
        cin >> player[i].number;
        cout << "Enter the players points :";
        cin >> player[i].points;
        cout << "------------------------------\n";
    	cin.ignore();

I used getline so a space is available for a full name.

Thanks again!

how it looks is not relevant. Does it work the way you want it to work? Yes -- then its ok. No then you need to do more work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.