如何将预定义数据类型的值存储到数组中并输出它

How to store values of a predefined data type into an array and output it

本文关键字:数组 输出 存储 定义数据类型      更新时间:2023-10-16

我正在尝试将数据类型 Emp 的名称、年龄和工资存储到一个数组中,并要求用户首先将它们全部输入,然后全部输出。

这就是使用普通数组的最终所需输出的样子

输入用户数:3

第一个输入复位*

第二个输入复位*

第三个输入复位*

--------------------------------------------------->>

显示信息

姓名年龄工资

QWe 69 420

德沃尔 42 6900

RT 24 6898

------------------------------------------------------------>>

至于输入的三名员工的信息。我得到了一个使用向量的解决方案,但我可以通过将其存储在数组中来做到这一点吗

//Current code
// Included header files
#include <iostream>
#include <string>
#include<algorithm>
#include<vector>
using namespace std;
// Global variable declaration
int sized = 0;
// Struct declaration
struct Employee
{
string name;
int age;
double salary;
};
// Protype
void Showinfo(Employee);
int main()
{
// Declaring a variable of type Employee
Employee Emp;
// Inputting the number of employees that are being inputted
cout << "Enter the number of the employees that you want to enter into the database: ";
cin >> sized;
cout << endl << endl;
// Reseting the screen
system("cls");
// For loop to get the name,age and salary of the given number of employees
std::vector<Employee> employees;
for (int i = 0; i < sized; i++)
{
cout << "Enter Full name of employee: ";
cin.ignore();
getline(cin, Emp.name);
cout << endl;
cout << "Enter age of employee: ";
cin >> Emp.age;
cout << endl;
cout << "Enter salary of employee: ";
cin >> Emp.salary;
cout << endl;
employees.push_back(Emp);
system("cls");
}
cout << "Namet" << "Aget" << "Wage" << endl;
for (const auto& e : employees) {
Showinfo(e);
}
cout << "n" << endl;
// Pause the console
cout << "The program has ended" << endl << endl;
system("pause");
return 0;
}
// To display/showcase the information received
void Showinfo(Employee Emp)
{
cout << Emp.name << 't'
<< Emp.age << 't'
<< Emp.salary << 't' << endl;
}

我正在尝试获得相同的输出,但使用 Employee 类型的数组来存储员工信息并在不使用可调整大小的数组/向量的情况下输出它们

就像在这段代码中一样,我确实需要一些帮助来创建一个循环,该循环显示我在声明整数大小后在全局范围内声明了一个 Employee 类型的数组。我如何存储这些值(员工信息:姓名,年龄和薪水并输出它们,还是不可能那样)

数组的基本操作是索引。索引是通过数组元素在数组中的位置来访问数组元素。C++ 中具有 N 个元素的数组具有位置从 0 到 N-1 的元素。索引操作用方括号指定,索引值写在里面,如array[0]array[N-1]array[i],其中 i 允许取 0 到 N-1 之间的值。

举个粗略的例子:

#include <iostream>
using namespace std;
struct Employee
{
string name;
int age;
double salary;
};
Employee employees[40];
int main()
{
int size;
cin >> size;
if (size > 40) {
return -1;
}
for (int i = 0; i < size; ++i) {
cin >> employees[i].name >> employees[i].age >> employees[i].salary;
}
for (int i = 0; i < size; ++i) {
cout << employees[i].name << " "
<< employees[i].age << " "
<< employees[i].salary << endl;
}
return 0;
}

希望您已经知道for (int i = 0; i < size; ++i)意味着什么。

这是最终代码

#include <iostream>
#include <iomanip>
using namespace std;
//Global variable declaration
const int arraysize = 40;
struct Employee
{
string name;
int age;
double salary;
};
//Protype
void Showinfo(Employee employees[40], int& size);
//Global array declaration
Employee employees[arraysize];
int main()
{
int size;
cout << "Input the size: ";
cin >> size;
cout << endl << endl;
// Reseting the screen
system("cls");
if (size > 40) {
return -1;
}
for (int i = 0; i < size; ++i) {
cout << "Enter a name: ";
cin >> employees[i].name;
cout << endl;
cout << "Enter age: ";
cin >> employees[i].age;
cout << endl;
cout << "Enter the salary: ";
cin >> employees[i].salary;
cout << endl;
system("cls");
}
cout << "Displaying Information" << endl;
cout << "----------------------------------" << endl;
Showinfo(employees, size);
cout << "----------------------------------" << endl;
cout << "n" << endl;
cout << "The program has ended" << endl << endl;
system("pause");
return 0;
}
void Showinfo(Employee employees[arraysize], int& size) {
for (int i = 0; i < size; ++i) {
if (i == 0)
{
cout << setw(4) << "Namet" << "Aget" << "Wage" << endl << endl;
}
cout << setw(4) << employees[i].name << 't'
<< employees[i].age << 't'
<< employees[i].salary << endl;
}
}