Results 1 to 2 of 2

Thread: [RESOLVED] Newbie on C++. Help in Structures

  1. #1

    Thread Starter
    Fanatic Member louvelle's Avatar
    Join Date
    Jun 2008
    Posts
    513

    Resolved [RESOLVED] Newbie on C++. Help in Structures

    Hi guys!

    I have a text file name "Employee.txt"..
    The text contains the following:
    Code:
    Juan_De_La_Cruz,A001,1
    Miriam_Bismonte,A002,1
    Gloria_Macapagal,A003,3
    Manny_Pacquiao,A004,3
    Jose_Rizal,A005,2
    What I wanted to do right now is I kinda put the data in the text file into a structure that I created so that I can easily access the files when I try to search for something like the name of the employee.

    Here is my code so far and I'm kinda stuck right now because I don't know what to do. Feel free to criticize my code because I'm new in c++ and it will help me improve a lot.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    struct Employees
    {
    	char EmployeeName[50];
    	char EmployeeID[10];
    	int Level;
    };
    
    int main ()
    {
    	using namespace std;	
    	int offset;
    	char EmpCode[20];
    	string line;
    
    	cout << "----Payroll System----" << endl;
    	cout << "Enter Employee Code: \n";
    	cin >> EmpCode;
    	
    	ifstream Employee;
    	Employee.open("employee.txt");
    	if(Employee.is_open())
    	{
    		while (!Employee.eof())
    		{
    			getline(Employee, line);
    			if ((offset = line.find(EmpCode, 0)) != string::npos)
    				cout << line << endl;
    		}
    
    		Employee.close();
    		
    	}
    	else
    		cout << "Unable to open this file" << endl;
    
    	return 0;
    }
    I learned how to code in C++ on youtube by watching thenewboston topics about C++ and a couple of google search.

    Note: I was actually trying to create a Payroll System so please ignore the part with the cin >> EmpCode.

    Manny Pacquiao once posted in his twitter:
    It doesn't matter if the grammar is wrong, what matter is that you get the message

  2. #2

    Thread Starter
    Fanatic Member louvelle's Avatar
    Join Date
    Jun 2008
    Posts
    513

    Re: Newbie on C++. Help in Structures

    I finally found a solution to my problem. What it does is it stores a record in the structure that I created if the computer finds the record inputted by the user...it took me a while but at least problem solve..
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    
    using namespace std;	
    
    struct newperson
    {
    	string EmployeeName;
    	string EmployeeID;
    	string Level;
    } ListEmp;
    
    int main ()
    {
    	
    	int offset;
    	char EmpCode[20];
    	string line;
    	size_t found;
    
    	cout << "----Payroll System----" << endl;
    	cout << "Enter Employee Code: \n";
    	cin >> EmpCode;
    	
    	ifstream Employee;
    	Employee.open("employee.txt");
    	if(Employee.is_open())
    	{
    		while (!Employee.eof())
    		{
    			getline(Employee, line);
    			if ((offset = line.find(EmpCode, 0)) != string::npos)
    				cout << line << endl;
    
    				istringstream iss(line);	
    				getline(iss, ListEmp.EmployeeName, ',');
    				getline(iss, ListEmp.EmployeeID, ',');
    				getline(iss, ListEmp.Level, ',');
    
    				break;
    				
    		}
    
    		Employee.close();
    		
    		cout << "Employee Name: " <<ListEmp.EmployeeName << endl;		
    		
    	}
    	else
    		cout << "Unable to open this file" << endl;
    
    	return 0;
    }

    Manny Pacquiao once posted in his twitter:
    It doesn't matter if the grammar is wrong, what matter is that you get the message

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width