Infinite Array?

The instructions to construct this program require that :
-Read an unknown amount of employees: last names, first names, employee IDs, job titles, hours worked, hourly wages, and claimed number of deductions.
-Store the above values in arrays
There are other instructions, of course, but I've covered that without issue in the code. Since I'm newer to arrays I decided to write all the functions out first. I'm now seeing that I cannot direct the code to restart at the top without making one large function that holds a loop. This isn't what the instructor wants. What should I do? Arrays are to be added because I'm still unsure on how to use them, so advice on this would also be welcome.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 // ------- Header Files -------
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace std;

// ------- Global Variables -------
ofstream paystub;
string lName, fName, jTitle;
int dClaim, hrWorked, ID, otHours,worked;
double hrWage, pay, payaftertnd, payafterOT,wage;
char endProg;
double tax = 1.4;
double deduc = .05;
double otPay = 1.25;	//one and a quarter Overtime pay
bool dontEnd = true;
// 

// ------- Define Functions -------
void gatherinfo(string fName, string lName, string jTitle);
void payamount(double otPay, double hrWage, int hrWorked, double payafterOT, int otHours);
void openStub(ofstream& paystub);
void filecheck(ofstream& paystub);
void closeSeq(char endProg);



// ------- Begin Functions -------



void openStub (ofstream& paystub)
{
	dontEnd = true;
	paystub.open("weekpay.txt");
}
// -------------------------------------------------------------------

void filecheck(ofstream& paystub)
{
	if (!paystub.is_open())
	{
		cout << "ERROR! File not Found." << endl;
		system("pause");

		exit(1);
	}
}
// -------------------------------------------------------------------

void gatherinfo(string fName[1], string lName, string jTitle)
{
	dontEnd = true;
	system("CLS");
	cout << "Please enter your first name: ";
		cin >> fName[1];
	cout << "Please enter your last name: ";
		cin >> lName;
	cout << "Please enter your job title: ";
	cin.ignore();
	getline (cin, jTitle);
	
	paystub << "Employee Name: " << fName << " " << lName << endl;
	paystub << "Employee Job Title: " <<jTitle<< endl;
}
// -------------------------------------------------------------------

void payamount(int dClaim, int hrWorked, int ID, double hrWage, double otPay, double payafterOT, int otHours, double tax, double payaftertnd, double deduc)
{
	dontEnd = true;
	system("CLS");
	cout << "Please enter your employee ID#: ";
	cin >> ID;
	cout << "Please enter the amount of hours you worked this week:";
	cin >> hrWorked;
	cout << "How many deductions do you have?: ";
	cin >> dClaim;
	paystub << "Employee ID#:" << ID << endl;
	paystub << "Employee claimed " << dClaim << " deductions." << endl;
	cout << "Please enter your hourly wage: ";
	cin >> hrWage;

	if (hrWorked > 40)
	{otHours = (hrWorked - 40);
	payafterOT = ((otHours * otPay) + (40 * hrWage));}

	else if (hrWorked <= 40)
	{payafterOT = hrWorked * hrWage;}

	if (dClaim == 0)
	{payaftertnd = (payafterOT - (tax * payafterOT));}

	else if (dClaim == 1)
	{payaftertnd = payafterOT - (.35 * payafterOT);}

	else if (dClaim == 2)
	{payaftertnd = payafterOT - (.3 * payafterOT);}

	else if (dClaim == 3)
	{payaftertnd = payafterOT - (.25 * payafterOT);}

	else if (dClaim == 4)
	{payaftertnd = payafterOT - (.20 * payafterOT);}

	else
	{payaftertnd = payafterOT - (.15 * payafterOT);}

	cout << fixed << showpoint << setprecision(2);
	paystub<< fixed << showpoint << setprecision(2);
	paystub << "Their pay for this week will be $" << payaftertnd << endl;
}

void closeSeq(char endProg)
{
	cout << "Do you wish to enter another employee's information? Enter x (lowercase required) if not," << endl;
	cout << " enter any other character to continue." << endl;
		cin >> endProg;
		if (endProg == 'x')
		{
			dontEnd != true;
			/// Add stuff later
		}
		else
		{
			dontEnd= true;
		}
}

// -------------------------------------------------------------------

// -------------------------------------------------------------------



// ------- Main -------
int main()
{
	openStub(paystub);
	filecheck(paystub);
	gatherinfo(fName, lName, jTitle);
	payamount( dClaim,  hrWorked,  ID,  hrWage,  otPay,  payafterOT,  otHours,  tax,  payaftertnd,  deduc);
	closeSeq(endProg);
}
nothing is infinite in a computer. but <vector> is an objected oriented array that can grow to fit a reasonable amount of data depending on your machine's free memory. I routinely put 5-10 GB into one. The site has a tutorial on vectors.
basically, though, simple access is variable[index] from 0 so the 5th one is variable[4] and variable.push_back(item) will insert into a growing vector, variable.size() will tell you how many you have. Declare may be weird to you, the format is vector<type> name. eg vector<int> myint;
Last edited on
-Read an unknown amount of employees: last names, first names, employee IDs, job titles, hours worked, hourly wages, and claimed number of deductions.

For this, you'd want a class or struct I'd imagine. If your professor wants you to take in all this information, then I'm sure you're expected to use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class employee
{
    std::string lastName;
    std::string firstName;

    std::string jobTitle;

    int ID;
    int hours;

// ... etc..

public:

   employee() {}

   employee(std::string Title, std::string last, std::string first)
   {
        ID = eID;        
        lastName = last;
        //etc..
   }

}



Then you'll want to make an array of this struct. Since you're taking in one employee at at time and don't know how many they'll want, you have a few options. You can use a vector:

1
2
3
#include <vector> //This is needed

std::vector<employee> track;


And every time you take in the information for an employee, you'll simply create a new object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void gatherinfo(string fName[1], string lName, string jTitle, std::vector<employee> &track)
{
	dontEnd = true;
	system("CLS");
	cout << "Please enter your first name: ";
		cin >> fName[1];
	cout << "Please enter your last name: ";
		cin >> lName;
	cout << "Please enter your job title: ";
	cin.ignore();
	getline (cin, jTitle);
	
	paystub << "Employee Name: " << fName << " " << lName << endl;
	paystub << "Employee Job Title: " <<jTitle<< endl;

        track.push_back(employee(jTitle, lName, fName)); //Saved Employee in Vector
}




If you can't use a vector, then I assume dynamic memory is what you're expected to use?:

http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
The instructions say to use arrays: use arrays.

Remember, unknown number != infinite.

Make yourself a reasonably large array and use it as usual, making sure to cap input at the size of the array. Your professor's input should not exceed it.

Unless you are expected to be able to handle dynamic. Memory management (which does not appear to be the case) then this should suffice. Assume a thousand or so element capacity.

Good luck!
Topic archived. No new replies allowed.