Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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

C++
#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
Posted
Updated 17-Feb-16 16:15pm

1 solution

To show everyone's age and salary, you also need to use your cout 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 the table elements, which you can redirect to a file and view using any Web browser. Or you would have to write a GUI.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900