我正在尝试将数据类型Emp的名称,年龄和工资存储到数组中,并要求用户首先全部输入,然后全部输出。
这是使用普通数组的最终所需输出的样子
要输入的用户数:3
第一个输入的重置信息*
第二个输入的重置信息*
第三位输入的重置信息*
-------------------------------------------------- ---- >>
显示信息
姓名年龄工资
QWe 69420
Dvor 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 << "Name\t" << "Age\t" << "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类型的数组来存储雇员信息并在不使用可调整大小的数组/向量的情况下输出它们
就像这段代码I do need some assistance with a creating a loop that displays the information一样,在声明整数大小后,我确实在全局范围内声明了一个Employee类型的数组。如何存储这些值(Employees信息:姓名,年龄和薪金并输出它们,或者不可能那样)
答案 0 :(得分:0)
数组的基本操作是索引。索引是通过数组元素在数组中的位置来访问它。在C ++中,具有N个元素的数组具有位置从0到N-1的元素。索引操作用方括号指定,索引值写在内部,例如array[0]
或array[N-1]
或array[i]
,其中i被允许采用0到N-1的值。 / p>
一个粗略的例子:
#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)
的含义。
答案 1 :(得分:0)
这是最终代码
#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) << "Name\t" << "Age\t" << "Wage" << endl << endl;
}
cout << setw(4) << employees[i].name << '\t'
<< employees[i].age << '\t'
<< employees[i].salary << endl;
}
}