如何传递2d数组并打印员工年龄,身份和工资 [英] How to pass 2d arrays and print the employee age, id, and salary
问题描述
您好,
我正在研究2D阵列,实际上我的代码正在运行,但我很难显示年龄,ID和我在主要功能中实施的薪水,
所以当我运行程序时,它只显示平均年龄和平均工资。
谢谢你的时间并帮助
这是我的代码
Hello ,
I'm working on 2d arrays and actually my code is working, but i'm having hard time to diplay the Age, ID , and salary as i implemented inside the main function,
so When i run the program it's only display the average age and the average salary.
Thank you for your time and help
here's my code
#include <iostream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
class Employee {
public:
Employee();//construct
void setAge(int x) {
age = x;
};
int Employee::getAge() {
return age;
};
void setId(int x) {
id = x;
};
int Employee::getId() {
return id;
};
void setSalary(float x) {
salary = x;
};
float Employee::getSalary() {
return salary;
};
void PrintEmployee(Employee arg1[][3], int arg2, int arg3);
private:
int age;
int id;
float salary;
};
Employee::Employee() {
age = 0;
id = 0;
salary = 0.0;
};
void Employee::PrintEmployee(Employee arg1[][3], int arg2, int arg3) {
int totalage = 0; ;
int totalsalary = 0;
float avargeAge = 0;
float avargeSalary = 0;
for (int row = 0; row < arg2; row++) {
for (int colm = 0; colm < arg3; colm++) {
totalage = totalage + arg1[row][colm].getAge();
totalsalary = totalsalary + arg1[row][colm].getSalary();
avargeAge = (totalage / 6);
avargeSalary= (totalsalary / 6);
}
}
cout << " The Average age is " << avargeAge << endl;
cout << " The Average salary is " << avargeSalary << endl;
}
int main() {
Employee x[2][3];
Employee object;
x[0][0].setAge(30); x[0][0].setId(111); x[0][0].setSalary(30000);
x[0][1].setAge(31); x[0][1].setId(112); x[0][1].setSalary(31000);
x[0][2].setAge(32); x[0][2].setId(113); x[0][2].setSalary(32000);
x[1][0].setAge(33); x[1][0].setId(114); x[1][0].setSalary(33000);
x[1][1].setAge(34); x[1][1].setId(115); x[1][1].setSalary(34000);
x[1][2].setAge(35); x[1][2].setId(116); x[1][2].setSalary(35000);
object.PrintEmployee(x, 2, 3);
system("pause");
return 0;
}
我的尝试:
当我运行程序时,它只显示平均年龄和平均工资。
我也希望程序显示表格以显示年龄,身份证和工资
感谢您的时间和帮助
What I have tried:
When i run the program it's only display the average age and the average salary.
also i want the program to display the table to show the Age, ID , and Salary
Thank you for your time and help
推荐答案
为了显示每个人的年龄和薪水,您还需要使用您显示的内部循环内的cout
。您已完成计算。
另请参阅:基本输入/输出 - C ++教程 [ ^ ]。
在本文中,特别是阅读有关换行符的说明(通过在末尾添加换行符搜索) - 您需要在行之间换行。 br />
请注意,如果要正确对齐列,整个想法不适合该任务;只有当你知道所有字段的字符串长度并且可以选择使用\t
时,它才有可能;在所有情况下,都会产生难看的结果。表格格式化的最简单方法是生成带有表
元素的HTML代码,您可以使用任何Web浏览器将其重定向到文件和视图。或者您必须编写GUI。
-SA
To show everyone's age and salary, you also need to use yourcout
inside the inner loop you've shown. You already done the calculations.
See also: Basic Input/Output - C++ Tutorials[^].
In this article, in particular, read the explanation about line breaks (search by "add line breaks at the end") — you will need line breaks between rows.
Note that the whole idea is not suitable to the task if you want to align columns properly; it's possible only if you know the string lengths of all fields and optionally use\t
; in all cases, expect ugly result. The simplest way of nice formatting of the table would be generating, for example, HTML code with thetable
elements, which you can redirect to a file and view using any Web browser. Or you would have to write a GUI.
—SA
这篇关于如何传递2d数组并打印员工年龄,身份和工资的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!