-
March 22nd, 2010, 12:59 PM
#1
Adding data to parallel array
Alright I have been struggling with this for a day or so now and I can't seem to figure out why junk is getting returned to me when adding a new entry to a parallel array.
Code:
void addEmployee(int hireDate[], int empID[], int salary[], int& numVals)
{
int newHireDate = 0;
int newEmpID = 0;
int newSalary = 0;
cout << "Please enter new employees data in this format:" << endl;
cout << "[hire date] [employee ID] [salary]" << endl << endl;
cin >> newHireDate >> newEmpID >> newSalary;
newHireDate = hireDate[numVals];
newEmpID = empID[numVals];
newSalary = salary[numVals];
numVals++; //Add 1 to current array values
for (int count = 0; count < numVals; count++)
{
cout << hireDate[count] << " " << empID[count] << " ";
cout << salary[count] << endl;
}
}
This is the function that I have. It is pretty self explanatory I assume, the arrays are being read in from a file and if you need the whole code I could post it as well.
added: Could this possibly be because I need to declare those integers inside of the function?
This is the output:
Code:
1. List by hire date
2. List by employee number
3. Write total of salaries
4. Add employee
5. Delete employee
Please select an option: 4
Please enter new employees data in this format:
[hire date] [employee ID] [salary]
500 500 500 <--- Numbers user input
20080505 563 66456
20080620 576 77456
20080730 123 88343
20080801 323 95474
20080820 753 74335
20080912 854 24353
20081015 238 53560
20081123 545 60965
20081129 467 78432
20081203 983 88865
20090105 776 65678
20090107 993 67784
-858993460 -858993460 -858993460 <----Numbers user got back. (Junk)
Press any key to continue . . .
This is the whole project:
Code:
// ------------
// Program Number 6 - Employee Data
// CST-180 C++ Programming I
// Winter 2010
// This program will sort and list employee data including hire date, employee number,
// and totals the salaries. It also lets you add or delete new employees.
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_ARRAY_SIZE = 25;
void getData(ifstream& inFile, int hireDate[], int empID[], int salary[], int& numVals);
void empIdArray(int hireDate[], int empID[], int salary[], int& numVals);
void showEmpId(int hireDate[], int empID[], int salary[], int& numVals);
void hireDateArray(int hireDate[], int empID[], int salary[], int& numVals);
void showHireDate(int hireDate[], int empID[], int salary[], int& numVals);
void totalSalary(int salary[], int& numVals);
void menu(ifstream& inFile, int hireDate[], int empID[], int salary[], int& numVals);
void addEmployee(int hireDate[], int empID[], int salary[], int& numVals);
int main()
{
ifstream inFile;
int numVals;
int hireDate[MAX_ARRAY_SIZE];
int empID[MAX_ARRAY_SIZE];
int salary[MAX_ARRAY_SIZE];
inFile.open("employdata.txt"); // opens the file
if (inFile.fail() )
{
cout << "Problem opening file";
exit (-1);
}
getData(inFile, hireDate, empID, salary, numVals);
menu(inFile, hireDate, empID, salary, numVals);
//addEmployee(hireDate, empID, salary, numVals,
// newHireDate, newEmpID, newSalary);
// Close file
inFile.close();
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function reads the file in and gets the values until the end of file
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void getData(ifstream& inFile, int hireDate[], int empID[], int salary[], int& numVals)
{
int currRec = 0; // Declares current recording
int totalRecs = 0; // Declares total recordings
inFile >> hireDate[currRec] >> empID[currRec] >> salary[currRec]; // Priming read
while (!inFile.eof() && currRec < MAX_ARRAY_SIZE)
{
currRec++;
inFile >> hireDate[currRec] >> empID[currRec] >> salary[currRec]; // Continuation read
}
totalRecs = currRec; // Finds total number of elements
numVals = totalRecs; // Sets number of elements in data file
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function creates a menu system for the user to choose options from.
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void menu (ifstream& inFile, int hireDate[], int empID[], int salary[], int& numVals)
{
int choice;
cout << "1. List by hire date" << endl;
cout << "2. List by employee number" << endl;
cout << "3. Write total of salaries" << endl;
cout << "4. Add employee" << endl;
cout << "5. Delete employee" << endl << endl;
cout << "Please select an option: ";
cin >> choice;
switch (choice)
{
case 1:
empIdArray(hireDate, empID, salary, numVals);
showEmpId (hireDate, empID, salary, numVals);
break;
case 2:
hireDateArray(hireDate, empID, salary, numVals);
showHireDate(hireDate, empID, salary, numVals);
break;
case 3:
totalSalary(salary, numVals);
break;
case 4:
addEmployee(hireDate, empID, salary, numVals);
break;
case 5:
// Do function
break;
default: cout << "There is a problem still";
}
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function lists the data by hire date.
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void hireDateArray(int hireDate[], int empID[], int salary[], int& numVals)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (numVals - 1); count++)
{
if (hireDate[count] > hireDate[count + 1])
{
temp = hireDate[count];
hireDate[count] = hireDate[count + 1];
hireDate[count + 1] = temp;
temp = empID[count];
empID[count] = empID[count + 1];
empID[count + 1] = temp;
temp = salary[count];
salary[count] = salary[count + 1];
salary[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function sorts the data by employee number.
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void empIdArray(int hireDate[], int empID[], int salary[], int& numVals)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (numVals - 1); count++)
{
if (empID[count] > empID[count + 1])
{
temp = hireDate[count];
hireDate[count] = hireDate[count + 1];
hireDate[count + 1] = temp;
temp = empID[count];
empID[count] = empID[count + 1];
empID[count + 1] = temp;
temp = salary[count];
salary[count] = salary[count + 1];
salary[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function displays by employee number (ascending) after sorted.
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void showEmpId(int hireDate[], int empID[], int salary[], int& numVals)
{
for (int count = 0; count < numVals; count++)
{
cout << hireDate[count] << " " << empID[count] << " ";
cout << salary[count] << endl;
}
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function displays by hire date (ascending) after sorted.
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void showHireDate(int hireDate[], int empID[], int salary[], int& numVals)
{
for (int count = 0; count < numVals; count++)
{
cout << hireDate[count] << " " << empID[count] << " ";
cout << salary[count] << endl;
}
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function is for adding a new employee.
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void addEmployee(int hireDate[], int empID[], int salary[], int& numVals)
{
int newHireDate = 0;
int newEmpID = 0;
int newSalary = 0;
cout << "Please enter new employees data in this format:" << endl;
cout << "[hire date] [employee ID] [salary]" << endl << endl;
cin >> newHireDate >> newEmpID >> newSalary;
newHireDate = hireDate[numVals];
newEmpID = empID[numVals];
newSalary = salary[numVals];
numVals++; //Add 1 to current array values
for (int count = 0; count < numVals; count++)
{
cout << hireDate[count] << " " << empID[count] << " ";
cout << salary[count] << endl;
}
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// This function is for totaling the salaries
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void totalSalary(int salary[], int& numVals)
{
int total = 0;
for (int count = 0; count < numVals; count++)
{
total = total + salary[count];
}
cout << endl;
cout << "The total of the " << numVals << " salaries is: $" << total;
cout << endl << endl << endl << endl;
}
Last edited by lepinat0rr; March 22nd, 2010 at 01:04 PM.
Reason: added information
-
March 22nd, 2010, 01:07 PM
#2
Re: Adding data to parallel array
Code:
// reading the new values
cin >> newHireDate >> newEmpID >> newSalary;
// now you overwrite them with the uninitialized values in the array
newHireDate = hireDate[numVals];
newEmpID = empID[numVals];
newSalary = salary[numVals];
guess you want
Code:
// put the values into array
hireDate[numVals] = newHireDate;
empID[numVals] = newEmpID;
salary[numVals] = newSalary;
Kurt
-
March 22nd, 2010, 01:08 PM
#3
Re: Adding data to parallel array
Isn't this the wrong way round ?
Code:
newHireDate = hireDate[numVals];
newEmpID = empID[numVals];
newSalary = salary[numVals];
Should it be this ?
Code:
hireDate[numVals] = newHireDate ;
empID[numVals] = newEmpID ;
salary[numVals] = newSalary;
your humble savant
-
March 22nd, 2010, 01:21 PM
#4
Re: Adding data to parallel array
Grrr... I tried both ways and it didn't work before.. Now it does.
Well, I feel dumb. Thanks you two for the very fast replies.
-
March 22nd, 2010, 01:58 PM
#5
Re: Adding data to parallel array
A few other things....
1) You should initialize numVals to 0.
2) You should add a check to ensure that you never try to stick more than MAX_ARRAY_SIZE elements into an array.
3) For some reason people really like checking the istream "specific failure" methods like fail(). I would say just do "if (!inFile)" and leave it at that. It covers more cases. Also, you should verify that the stream extractions succeed before adding elements to your arrays.
4) Presumably there's some reason why you need to use parallel arrays here (assignment specification?), but you should know that using an array of structs is usually preferable.
-
March 22nd, 2010, 02:20 PM
#6
Re: Adding data to parallel array
Thank you for all of the tips and information Lindley. Yes, this is an assignment and it has to be a parallel array, plus I haven't learned about an array of structs yet but while googling I kept finding posts where people say use them over a parallel array whenever possible.
Thanks all again, now I just need to make the delete function and then go over it all!
-
March 22nd, 2010, 04:31 PM
#7
Re: Adding data to parallel array
As far as the layout in memory is concern, the difference between parallel arrays and an array of structs is essentially:
PA:
AAAAABBBBBCCCCCDDDDD
AoS:
ABCDABCDABCDABCDABCD
The latter is preferable for many reasons, but one obvious one is that you're probably going to use the third A close to the third C, so it would be nice if they were close together in memory so you'd get fewer cache misses.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|