如何将预定义数据类型的值存储到数组中并输出它
How to store values of a predefined data type into an array and output it
我正在尝试将数据类型 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;
}
}
相关文章:
- 将值从二维数组输出到文本文件
- 为什么我的数组输出一个不在其中的元素
- 长度为 20 的 Cout'ing 数组输出 23 个字符
- 将矢量数组输出到 tex 文件中,错误
- 来自不同位置的数组输出的执行时间
- 数组输出中缺少字符
- 字符数组输出 - C++
- C 合并的数组输出错误
- 制作垫子类型对象的数组.输出窗口显示同一帧
- C++数组输出地址
- 字符数组输出正确,但实际上没有存储正确的值
- 字符串数组输出
- 为什么在将字符数组输出到控制台时会得到垃圾值
- 字符数组输出额外的字符
- 从字符数组输出到控制台
- 为什么计数从字符数组输出错误的字符
- 动态初始化的 2D 数组输出错误
- C++指针和2d数组输出
- c++中奇怪的char数组输出
- 类函数中的c++字符数组输出