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

Dani AI

Generated

Short summary and practical fixes

The crash was not caused by integer addition itself but by undefined behaviour from your loop and the way input was mixed. As noted, the total-calculation loop used a condition that never becomes false, so the index runs past the array bounds and eventually the program crashes. Fixing the loop condition to compare the loop index against the size removes the out‑of‑bounds access.

About the input handling: mixing formatted extraction (operator>>) for numbers with line-oriented reads for names leaves the newline in the stream and can make the next line read behave unexpectedly. Two robust options: always use line-oriented input (read whole lines into std::string and parse numbers), or after a formatted extraction discard the remainder of that line before doing a line read. See the behavior and usage notes for istream::ignore and line-based reads on cppreference:

Safer C++ practices to avoid these pitfalls: prefer std::string over fixed-size char[] to avoid buffer overruns; use std::vector or std::array for collections instead of raw C arrays; validate the stream state after extraction (check for failbit) and handle parse errors; use a range-based loop or std::accumulate to compute totals for clarity.

Quick checklist

  • Correct the loop condition so it uses the loop index.
  • Don’t read past array bounds.
  • Decide on a single input style (line-based or token-based) and handle the leftover newline if you mix styles.
  • Replace C-style arrays with std::string/STL containers for safety and clarity.

’s switch to a line read for names eliminated the immediate symptom; the above points make the solution robust for real-world input.

Recommended Answers

All 3 Replies

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.