2 Replies - 1338 Views - Last Post: 10 April 2008 - 01:14 PM Rate Topic: -----

#1 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

I/O File - Data Input Sorting Question

Post icon  Posted 10 April 2008 - 11:12 AM

So I have been asked to create a application that can write data to a file and then allow the user to also update that data. The fallowing data is written:
(1) Employee Name
(2) Employee Number
(3) Start Date
(4) Part time or full time

I have my code so it can write this data to the file, but the problem comes with the user being able to update it. Since I will have more then one employee in their and their information I cant just read the data back in, in order. I need to be able to search based on their employee number or name and then pull the information related to that. So if I enter John Smith, it will search threw the file and find his name and then can read the information into the application below it. In a nutshell I need to be able to search threw my data file based on a name or a number, which I am unsure how to go about doing.

Here is my code so far:
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int menu();
void create(fstream &dataFile);
void update(fstream &dataFile);
//void remove(fstream &dataFile);
//void save(fstream &dataFile);


void main()
{
	int choice = 0;

	fstream dataFile;										// Creates dataFile stream
	dataFile.open("empSalesRecord.txt",ios::out | ios::in);			// Opens dataFile stream and creates file empSalesRecord.dat for output
	if(!dataFile.is_open())											   // Checks to make sure data file opened correctly					   
	{
		cout << "\nError: File could not be opened" <<endl;
		exit(1);
	}
		
	choice = menu();

	if(choice == 1)
		create(dataFile);
	if(choice == 2)
		update(dataFile);
//	if(choice == 3)
//		remove(dataFile);
	dataFile.close();

}

int menu() // Menu
{
	int choice = 0;

	cout << "MENU" <<endl;
	cout << "(1) Create sales person's record" <<endl;
	cout << "(2) Update sales person's recrod" <<endl;
	cout << "(3) Delete sales person's record" <<endl; 

	cout << "Please enter in a choice from the menu" <<endl;
	cout << "Choice: ";
	cin >> choice;

	/*while( choice != 1 && choice !=2 && choice !=3)
	{
		cout << "Error:	" <<endl;
	}*/
	
	return choice;
}

void create(fstream &dataFile) // Create employee record
{
	string empName; 
	string empStatus;
	double empNumber = 0, startYear = 0;
	char save;
	
	cin.ignore();

	cout << "CREATE RECORD" <<endl;
	cout << "Please enter in the employees name, number, start year, and status" <<endl;
	cout << "Part or Full Time" <<endl;
	cout << "Employees Name: ";
	getline(cin,empName);

	cout << "Employees Number: ";
	cin >> empNumber;
	cout << "Employees Start Year: ";
	cin >> startYear;
	cin.ignore();
	cout << "Employees Status (Part or Full Time): ";
	getline(cin,empStatus);

	cout << "Is the fallowing data correct" <<endl;
	cout << "Employees Name: "<< empName <<endl;
	cout << "Employees Number: "<< empNumber <<endl;
	cout << "Employees Start Year: "<< startYear <<endl;
	cout << "Employees Status: "<< empStatus <<endl;

	cout << "Would you like to save this data to file" <<endl;
	cout << "Save (Y/N): ";
	cin >> save;

	while(save != 'Y' && save != 'y' && save != 'N' && save != 'n')
	{
		cout << "Error: You must select Y or N" <<endl;
		cin >> save;
	}
	
	if(save == 'Y' || save == 'y' || save == 'N' || save == 'n')
	{
		dataFile << empName <<endl;
		dataFile << empNumber <<endl;
		dataFile << startYear <<endl;
		dataFile << empStatus <<endl;
	}


}
void update(fstream &dataFile) // Allows user to update information from the file
{
	string empName; 
	string empStatus;
	double empNumber = 0, startYear = 0;
	char save;

	// the fallowing could be used if I was only 1 person was in the file, but if their are more then 1 employee I need to search to find them
	cout << "Update Records" <<endl;

	getline(dataFile,empName);
	dataFile >> empNumber;
	dataFile >> startYear;
	dataFile.ignore();
	getline(dataFile,empStatus); 

	cout << "Employees Name: "<< empName <<endl;
	cout << "Employees Number: "<< empNumber <<endl;
	cout << "Employees Start Year: "<< startYear <<endl;
	cout << "Employees Status: "<< empStatus <<endl;
	

}
/*void remove(fstream &dataFile)
{


}*/



What commands or method could I possibly uses to search for their name in the data file?

Is This A Good Question/Topic? 0
  • +

Replies To: I/O File - Data Input Sorting Question

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: I/O File - Data Input Sorting Question

Posted 10 April 2008 - 01:00 PM

The only method I can think of to hand would be to read one character and check to see if the following letters match the inputted string. So, the theory behind it would look something like this:
char currentchar; //use it to read the file
string userinput; //string to search for
int inlen = strlen(userinput); //length of userinput
bool same = false; //make true if currentchar is the 
                            //same as userinput[0]

//read through the file character by character

if (currentchar == userinput[0])
{
    same = true;
    while (same == true)
    {
        for (int i = 1; i <= inlen; i++)
        {
            if (currentchar != userinput[i])
                same = false; //exits loop
            currentchar++; //continues to read the file
        }
        //the rest of the while loop will only execute if the for
        //loop was completed successfully (ie. string has been
        //found...
        //if the for loop completed successfully, the current line
        //of the file is the one that needs selecting to edit
    }
}


Sorry if this doesn't help, it kinda had me stumped =/
That's all I can think of, I think it would work, but more than anything it just depends on my explanation :P

As for starting from the beginning of the current line, you would just read back to where the character is '\n' (the end of the previous line)

Well, I hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: I/O File - Data Input Sorting Question

Posted 10 April 2008 - 01:14 PM

That is a good idea, but I believe their is another way to do it that is easier.

I asked my friend who is a C++ programmer so im going to see what his response is.

Thanks for the idea though, kinda sparked an idea of my own.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1