Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii, everybody
this code manage employee data (hiredate,no,salary,total-salary)

file which i write doesn't display anything?!

C#
// This program manages employee data
// It reads a file of employee information and offeres
// several difference sorting options
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void GetList(ifstream& fileIn, int hiredate[], int employee[], int salary[], int& totalRecs);
void menuaction(int hiredate[], int employee[], int salary[], int totalRecs);
void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs);
void sortarraybyID(int hiredate[], int employee[], int salary[], int totalRecs);
void showarray(int hiredate[], int employee[], int salary[], int totalRec);
int sum(int salary[], int totalRecs);
void Insert(int hiredate[], int employee[], int salary[], int& totalRecs);
void Delete(int hiredate[], int employee[], int salary[], int& totalRecs);


const int MAX_LIST_SIZE = 25;

int main()
{
	// Array of list elements 
	int hiredate[MAX_LIST_SIZE];
	int employee[MAX_LIST_SIZE];
	int salary[MAX_LIST_SIZE];
	int totalRecs = 0;

	// Set up file 
	ifstream fileIn;
	string inFileName = "C:\\Users\\MOHAMED\\Desktop\\xx.txt";
	fileIn.open(inFileName); //Open The File

	if (fileIn.fail()) // Test for file existence 
	{
		cout << "Problem opening file";
		exit(-1);
	}


	ifstream theData;

	GetList(fileIn, hiredate, employee, salary, totalRecs);

	menuaction(hiredate, employee, salary, totalRecs);

}

// This function reads integers from a file and stores the values in
// an array. It returns the loaded array and the number of elements
// in the array

void GetList(ifstream& InFile, int hiredate[], int employee[], int salary[], int& totalRecs)
{
	int i = 0;

	// Priming read 

	InFile >> hiredate[i] >> employee[i] >> salary[i];

	while (!InFile.eof())
	{
		i++; // Add one to pointer
			 // continuation reads
		InFile >> hiredate[i] >> employee[i] >> salary[i];
	}

	totalRecs = i;

}
// This Fuction is the Menu Action
void menuaction(int hiredate[], int employee[], int salary[], int totalRecs)
{

	int choice, Sum;

	do
	{
		cout << "\n\t\tEmployee Data Menu\n\n";
		cout << "1. List by hiredate\n";
		cout << "2. List by employee number\n";
		cout << "3. Write total of salaries\n";
		cout << "4. Add employee\n";
		cout << "5. Delete employee\n";
		cout << "6. TERMINATE SESSION\n";
		cout << "Enter your choice: ";

		cin >> choice;
		if (choice >= 1 && choice <= 5)
		{
			switch (choice)
			{
			case 1: DisplayList(hiredate, employee, salary, totalRecs);
				break;
			case 2: sortarraybyID(hiredate, employee, salary, totalRecs);
				showarray(hiredate, employee, salary, totalRecs);
				break;
			case 3: Sum = sum(salary, totalRecs);
				cout << "Sum = " << Sum << endl;
				break;
			case 4: Insert(hiredate, employee, salary, totalRecs);
				DisplayList(hiredate, employee, salary, totalRecs);
				break;
			case 5: Delete(hiredate, employee, salary, totalRecs);
				DisplayList(hiredate, employee, salary, totalRecs);
				break;


			}

		}
		else if (choice != 6)
		{
			cout << "The valid choices are 1 through 6.\n";
			cout << "Please try again.\n";
		}
	} while (choice != 6);

}
//This function writes a list to console output
void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs)
{
	for (int i = 0; i < totalRecs; i++)

		cout << hiredate[i] << "  " << employee[i] << "  " << salary[i] << "  " << endl;
	cout << endl;
}
// This functions Lists the employees by number using sorting 
void sortarraybyID(int hiredate[], int employee[], int salary[], int totalRecs)
{
	int temp, temp2, temp3, end;
	for (end = totalRecs - 1; end >= 0; end--)
	{
		for (int i = 0; i < end; i++)
		{
			if (employee[i] > employee[i + 1])
			{
				temp = employee[i];
				temp2 = hiredate[i];
				temp3 = salary[i];
				employee[i] = employee[i + 1];
				hiredate[i] = hiredate[i + 1];
				salary[i] = salary[i + 1];
				employee[i + 1] = temp;
				hiredate[i + 1] = temp2;
				salary[i + 1] = temp3;
			}
		}
	}
}
void showarray(int hiredate[], int employee[], int salary[], int totalRecs)
{
	for (int i = 0; i < totalRecs; i++)
		cout << hiredate[i] << "  " << employee[i] << "  " << salary[i] << endl;
}
// This functions writes the total of the salaries
int sum(int salary[], int totalRecs)
{
	int sum = 0;

	for (int i = 0; i <= totalRecs; i++)
		sum += salary[i];

	return sum;
}

// This function receives an integer, an array containing   
// an unordered list, and the size of the list.  It inserts 
// a new integer item at the end of the list
void Insert(int hiredate[], int employee[], int salary[], int& totalRecs)
{
	int newint, newint2, newint3;


	cout << "Insert New Employee HIREDATE:" << endl;
	cin >> newint;
	hiredate[totalRecs] = newint;
	cout << "Insert New Employee ID NUMBER:" << endl;
	cin >> newint2;
	employee[totalRecs] = newint2;
	cout << "Insert New Employee's Salary:" << endl;
	cin >> newint3;
	salary[totalRecs] = newint3;

	totalRecs++; // Increment size of list
}


// This function receives an integer, an array containing
// an unordered list, and the size of the list. It locates
// and deletes the integer fromt he list
void Delete(int hiredate[], int employee[], int salary[], int& totalRecs)
{
	int empnum = 0, ptr = 0;

	cout << " Which employee do you wish to delete" << endl;
	cin >> empnum;

	// Scan list for deletion target 
	while (empnum != employee[ptr] && ptr < totalRecs)

		ptr++;

	if (ptr < totalRecs) // If target found, then 
	{
		employee[ptr] = employee[totalRecs - 1]; // Last list item to overwrite target 
		hiredate[ptr] = hiredate[totalRecs - 1];
		salary[ptr] = salary[totalRecs - 1];
		totalRecs--; // Decrement size of list 
	}
}

What I have tried:

i tried to edit path but fail!
plz if someone try to help me
Posted
Updated 3-Nov-16 22:38pm
v2
Comments
[no name] 3-Nov-16 18:19pm    
Learn to use the debugger.
Philippe Mori 3-Nov-16 23:04pm    
And also write the code a small portion at a time and test it.

I don't see code that write to a file. Where is such code and what do you expect and in which file?
mhassan083 4-Nov-16 12:15pm    
this is a file sir
string inFileName = "C:\\Users\\MOHAMED\\Desktop\\xx.txt";
fileIn.open(inFileName);

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Your get function is incorrect as it will always return an incorrect record count. It should be:
C++
void GetList(ifstream& InFile, int hiredate[], int employee[], int salary[], int& totalRecs)
{
	int i = 0;
 
	// Priming read 

	InFile >> hiredate[i] >> employee[i] >> salary[i];
 
	while (!InFile.eof())
	{
		i++; // Add one to pointer
			 // continuation reads
		InFile >> hiredate[i] >> employee[i] >> salary[i];
	}
 
	totalRecs = i + 1;
 
}

You also forgot to add any code to write the results out to a file.
 
Share this answer
 
Comments
mhassan083 4-Nov-16 11:00am    
after edit method (GetList) nothing appear in the file ?!
Richard MacCutchan 4-Nov-16 11:01am    
What file? There is nothing in your program that writes to a file.
mhassan083 4-Nov-16 12:16pm    
this is a file sir
string inFileName = "C:\\Users\\MOHAMED\\Desktop\\xx.txt";
fileIn.open(inFileName);
Richard MacCutchan 4-Nov-16 12:54pm    
Yes I can see that but you are reading it, not writing.
mhassan083 4-Nov-16 16:24pm    
how can make it writing sir

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