На самом деле вам нужен контейнер с переменным размером для хранения всех введенных сотрудников.
Более подходящим контейнером в вашем случае является std::vector
.
Вот демонстрационная программа,
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
struct Employee
{
std::string name;
int age;
double salary;
};
std::ostream & Showinfo( const Employee &employee, std::ostream &os = std::cout );
int main()
{
size_t n = 0;
std::cout << "Enter the number of employees you want to enter into the database: ";
std::cin >> n;
std::cout << '\n';
std::vector<Employee> employees;
employees.reserve( n );
for ( size_t i = 0; i < n; i++ )
{
Employee emp;
std::cout << "Enter Full name of employee: ";
std::cin.ignore();
std::getline( std::cin, emp.name );
std::cout << '\n';
std::cout << "Enter age of employee: ";
std::cin >> emp.age;
std::cout << '\n';
std::cout << "Enter salary of employee: ";
std::cin >> emp.salary;
std::cout << '\n';
employees.push_back( emp );
}
// To display the elements of the information given
std::cout << "\nDisplaying Information.\n";
std::cout << "--------------------------------\n";
for ( const Employee &emp : employees ) Showinfo( emp );
return 0;
}
//To display/showcase the information received
std::ostream & Showinfo( const Employee &emp, std::ostream &os )
{
os << "Name: " << emp.name << '\n';
os << "Age: " << emp.age << '\n';
os << "Salary: " << emp.salary << '\n';
return os << std::endl;
}
Его вывод может выглядеть как
Enter the number of employees you want to enter into the database: 2
Enter Full name of employee: ball
Enter age of employee: 69
Enter salary of employee: 420
Enter Full name of employee: Rally
Enter age of employee: 42
Enter salary of employee: 690000
Displaying Information.
--------------------------------
Name: ball
Age: 69
Salary: 420
Name: Rally
Age: 42
Salary: 690000