Search code examples
arraysstructure

My structure array only shows the last input - c++


So I'm trying to use a structure array, where you input your own values into each variable. The general idea of it is that I want to have the user input 5 different employee names, IDs and annual salaries. The program is intended to output the structure array.

However, my issue is that when I try to output the array, the output seems to only show the last element of the array 5 times over.

All help is appreciated!

p.s. Don't worry about the average function, I wanna figure that one out on my own.

    #include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

struct emp
{
    char empName[25];
    int id;
    float salary;
};

struct emp employee[5];

struct emp *getData();
void printData(struct emp employee[]);


int main()
{   
    cout << "Enter employee name, ID and Annual Salary: " << endl;
    struct emp *p;
    p = getData();


    printData(employee);


}

struct emp *getData()
{
    for (int i = 0; i < 5; i++)
    {
        cout << "Employee " << i + 1 << " name: ";
        cin >> employee->empName;
        cout << "Employee " << i+1 << " ID: ";
        cin >> employee->id;
        cout << "Employee " << i+1 << " Annual Salary: ";
        cin >> employee->salary;
        cin.ignore();
        cout << endl;
    }

    return employee;
}

void printData(struct emp employee[])
{
    for (int i = 0; i < 5; i++)
    {
        cout << "Employee " << i + 1 << ": " << employee->empName << " - " << employee->id << ", Annual Salary: " <<
            employee->salary << endl;
    }
}

//double getAverage(struct emp[], int)
//{
//
//}

Output

Enter employee name, ID and Annual Salary:
Employee 1 name: Ian
Employee 1 ID: 2345
Employee 1 Annual Salary: 12345

Employee 2 name: John
Employee 2 ID: 2345
Employee 2 Annual Salary: 23456

Employee 3 name: Carla
Employee 3 ID: 23452
Employee 3 Annual Salary: 2345

Employee 4 name: Chloe
Employee 4 ID: 12345
Employee 4 Annual Salary: 2345

Employee 5 name: Josh
Employee 5 ID: 234
Employee 5 Annual Salary: 12345

Employee 1: Josh - 234, Annual Salary: 12345
Employee 2: Josh - 234, Annual Salary: 12345
Employee 3: Josh - 234, Annual Salary: 12345
Employee 4: Josh - 234, Annual Salary: 12345
Employee 5: Josh - 234, Annual Salary: 12345
Press any key to continue . . .

Solution

  • You are not using any array blocks so all input is going into the first and all output coming from the first.

    for (int i = 0; i < 5; i++)
        {
            cout << "Employee " << i + 1 << " name: ";
            cin >> employee[i].empName;
            cout << "Employee " << i+1 << " ID: ";
            cin >> employee[i].id;
            cout << "Employee " << i+1 << " Annual Salary: ";
            cin >> employee[i].salary;
            cin.ignore();
            cout << endl;
        }
    
        return employee;
    }
    
    void printData(struct emp employee[])
    {
        for (int i = 0; i < 5; i++)
        {
            cout << "Employee " << i + 1 << ": " << employee[i].empName << " - " << employee[i].id << ", Annual Salary: " <<
                employee[i].salary << endl;
        }
    }
    

    This should fix your code