Need Major Help!!! Beginner C++

I am trying to right a program where I consider a Employee with data:

age

(an integer),

id

(an integer),

salary

(a float). I want to create three one-dimensional arrays with length 3. Then I would want cout asking for the input values for the elements and use cin to grab users' input from keyboard. Next, pass the three arrays to a function, PrintEmployee(arg1, arg2, arg3, arg4), in which arg1 through arg3 should be the arrays of Employee and arg4 is the length of the array (=3 in this case). In PrinteEmployee(arg1,arg2, arg3, arg4). Im trying to right a for loop to prnt out the information of each employee. And I also want to print avg salary and avg age. I really need help!! Here is my code so far:

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

void main()
{
int Age [3];
int Id [3]; //create an array of Employee with three elements
float Salary [3];


cout << "What is the employee Age, Id number,and Salary:"; //ask user from keyboard for each element
cin>>Age
cin>>Id
cin>>Salary

Age [0] = 30; // set values for the three elements
Age [1] = 30042554;
Age [2] = 50000.00;

Id [0] = 45;
Id [1] = 40041002;
Id [2] = 70000.00;

Salary [0] = 25;
Salary [1] = 50051003;
Salary [2] = 30000.00;

PrintEmployee(Age,Id,Salary); //call PrintEmployee() with a proper paramater list

}

void PrintEmployee(int a[], int b[], float c[])
{
for (int i=0;i<3;i++) //for loop
{
int AvgSalary = float c [0]+float c [1]+float c[2]/3;
int AvgAge = int a[0] + int a[1] + int a[2]/3;

cout<<a[i]<<"Employee 1: "<<int a[0],int b[0],float c[0]<<endl;//Output the information to screen by calling the member functions of the struct
cout<<b[i]<<"Employee 2: "<<int a[1],int b[1],float c[1]<<endl;
cout<<c[i]<<"Employee 3: "<<int a[2],int b[2],float c[2]<<endl;
cout<<"Average Salary: "<<AvgSalary<<endl;//Print out the average salary
cout<<"Average Age: "<<AvgAge<<endl; //print out the average age
}

It probably totally wrong but I need help at least getting on the right track.
Please, try to understand the changes and modify the program as you wish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

void PrintEmployee(int a[], int b[], float c[])
{
	for (int i=0;i<3;i++) //for loop
	{
		cout<< "\nEmployee " << i + 1 << endl;//Output the information 
		cout << "-----------" << '\n';
		cout << "Age: " << a[i] << endl;
		cout<< "Id: " << b[i] << endl;
		cout<< "Salary: " << c[i]<< endl;
		cout << "==========================" << '\n';
	}
	
	float AvgSalary = (c[0] + c[1] + c[2]) / 3;
	int AvgAge = (a[0] + a[1] + a[2]) / 3;
	cout<<"Average Salary: "<<AvgSalary<<endl;//Print out the average salary
	cout<<"Average Age: "<<AvgAge<<endl; //print out the average age
}

int main()
{
	int Age [3];
	int Id [3]; //create an array of Employee with three elements
	float Salary [3];

	Age[0] = 30; // set values for the three elements
	Id[0] = 30042554;
	Salary[0] = 50000.00;

	Age[1] = 45;
	Id[1] = 40041002;
	Salary[1] = 70000.00;

	Age[2] = 25;
	Id[2] = 50051003;
	Salary [2] = 30000.00;

	PrintEmployee(Age, Id, Salary); //call PrintEmployee() with a proper paramater list
	cin.get(); //waits for a key;
	return 0;
}
Employee 1
-----------
Age: 30
Id: 30042554
Salary: 50000
====================

Employee 2
-----------
Age: 45
Id: 40041002
Salary: 70000
====================

Employee 3
-----------
Age: 25
Id: 50051003
Salary: 30000
====================
Average Salary: 50000
Average Age: 33



Last edited on
Thank you so much. I see the difference. But can I ask why you used cin.get instead of a regular cin >>?
Because it is the end of the program and the console "disappears" in the very moment. So, cin.get() will wait for you press the return key. This is for the windows platform. No need for the Linux platform this "trick".
Topic archived. No new replies allowed.