#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

struct employees
{
       int em_id;
       string em_name;
       string position;
       string gender;
       
};
       

void read(employees record[], int max_1);

bool IDsort(const  employees &a, const  employees &b)
{ 
     return(a.em_id < b.em_id);

}

char Namesort(const  employees&a, const  employees &b)
{
     return(a.em_name < b.em_name);
     
}


void Insort(employees record[], int max_2);

void printemployees ( employees record[], int max_3);

 
int main()
{ 
    
    const int n= 1000;
    employees record[n];
    
    int options;
    
    
   
    
         cout<<"-------------------------------------------------------------------------------"<<endl;
         cout<<"Welcome to Employees Record System"<<endl;
         cout<<"Please insert a number to proceed"<<endl;
         cout<<"-------------------------------------------------------------------------------"<<endl;
         cout<<" 1.Insert new record."<<endl;
         cout<<" 2.Sort record."<<endl;
         cout<<" 3.Delete record."<<endl;
         cout<<" 4.Display record."<<endl;
         cout<<" -------------------------------------------------------------------------------"<<endl;
         cout<<"Selection : ";
         cin>>options;
       
         switch(options)
         {
             case 0: break;
             case 1: read(record,n);
                     break;
             case 2: Insort(record,n);
                     break;
             
             case 4: printemployees(record,n);
                     break;
             default: cout<<"Invalid selection"<<endl;
                      break;         
         
         }                
        
        
                  
   
    system ("pause");
    return 0;
}
    
   
void read( employees record[], int max_1)
{  
      
     for (int i=0; i<max_1; i++)
     {
       
         for (int i=0; i<max_1; i++)
         {   
           continued:
             cout<<"employees no. "<<(i+1)<<endl;
              
             cout<<"Enter  employee ID: ";
             cin>>record[i].em_id;
             cin.ignore(100,'\n');
             
             cout<<"Enter  employee name: ";
             cin>>record[i].em_name;
             cin.ignore(100,'\n');
             
             cout<<"Enter  employee position: ";
             cin>>record[i].position;
             cin.ignore(100,'\n');
             
             cout<<"Enter employee gender: ";
             cin>>record[i].gender;
             cin.ignore(100,'\n');
             
             
             }
         }
}

void Insort(employees record[], int max_2)
{
     int selections=0;
     cout<<"-------------------------------------------------------------------------------"<<endl;
     cout<<"You had selected sorting function"<<endl;
     cout<<"Select a number to proceed"<<endl;
     cout<<"-------------------------------------------------------------------------------"<<endl;
     cout<<"1. Sort by ID"<<endl;
     cout<<"2. Sort by name"<<endl;
     cout<<"-------------------------------------------------------------------------------"<<endl;
     cout<<"Selection: "<<endl;
     cin>>selections;
     
     switch(selections)
     {
        case 1: std::sort(record,record+max_2,IDsort);
                break;
                   
        case 2: std::sort(record,record+max_2,Namesort);
                break;
                       
     }
     
}

void printemployees(employees record[], int max_3)
{
     for(int i=0; i<max_3; i++)
     {
             cout<<"employee no. "<<(i+1)<<endl;
             cout<<"employee ID: "<<record[i].em_id<<endl;
             cout<<"employee name: "<<record[i].em_name<<endl;
             cout<<"employeenationality: "<<record[i].position<<endl;
             cout<<"employee gender: "<<record[i].gender<<endl;
             
     }
}

Hi..i have modified someone's code and try to understand it..
But for this record system..there are several problems

1) The record can't quit when the user want to.
2) The record can't update and when want to display it, it fail.

Any suggestion to do that? I am not asking the coding..just the way to do that. Thanks for any help or suggestion ^^''

1 & 2) Extend the menu to have Exit and Update options and just follow same logic already in place

2) when processing the Update option case label, get input for employee number, search the array for that record, then just pass that record to the read function. You might want to add extra code in the read function though:
a) have an extra parameter in the read function definition that indicates edit mode
b) when editing, display current value and ask if they want to change it; if they want to change it ('Y' entered or something), then allow the current code for that field to execute as currently defined; else, skip to the question/input for the next field.

Cheers!

1 & 2) Extend the menu to have Exit and Update options and just follow same logic already in place

2) when processing the Update option case label, get input for employee number, search the array for that record, then just pass that record to the read function. You might want to add extra code in the read function though:
a) have an extra parameter in the read function definition that indicates edit mode
b) when editing, display current value and ask if they want to change it; if they want to change it ('Y' entered or something), then allow the current code for that field to execute as currently defined; else, skip to the question/input for the next field.

Cheers!

Hmm..i am confused LOL..i will google it and try it out. Thanks for u suggestions.
However,
"You might want to add extra code in the read function though:
a) have an extra parameter in the read function definition that indicates edit mode"

this part i don't really understand it..can explain it?

If the person you are copying is who I think it is, be aware that there are many flaws in his code. He would ask questions, get answers, but do a seemingly random copy and paste of the answers, but often in an incorrect way, so the end result, often, is code that doesn't make sense. So if you are trying to learn, realize that the code you are trying to modify was often incorrect in the first place. It might be better to find a different source.

You don't have an option to exit because none is implemented in this code. Look at the menu:

cout<<" 1.Insert new record."<<endl;
         cout<<" 2.Sort record."<<endl;
         cout<<" 3.Delete record."<<endl;
         cout<<" 4.Display record."<<endl;

Add an option to exit, then add a "case" to the switch statement to handle it.

Display record isn't going to work till you read in the records. Reading in the records was one of this person's big problems and it still is with the code you posted. In fact, it looks worse now than when I remember it.

Unless you feel like entering 1000 records from your keyboard, you're going to have to change this. 1000 records is way too many if you are trying to learn from scratch. Have the maximum be 5. Inserting a record is completely incorrect. There should be no loop. The task was to insert a record, meaning one. No loop.

So, if you're a complete newbie and have no idea how the program works, I'd scratch this program and find another one to study. This program can certainly be fixed, and it has potential (many things are correct), but it also has a lot of problems and needs a considerable overhaul.

Hmm..i am confused LOL..i will google it and try it out. Thanks for u suggestions.
However,
"You might want to add extra code in the read function though:
a) have an extra parameter in the read function definition that indicates edit mode"

this part i don't really understand it..can explain it?

Sure. Currently, read is defined as:

void read( employees record[], int max_1)

To expand it's usage to include update option:

void read( employees record[], int max_1, bool bUpdate=TRUE)

Then, add additional logic to the body. You can just set the max_1 var to 1, but I would recommend actually taking out the loops here and putting them around the call to read, passing in just the record being added/updated. Anyway to do update with separate read routine:

void read2( employees emp, bool bUpdate)
{  
	cout<<"employees no. "<< emp.em_id <<endl;

	char ch = 'Y';

	if (!bUpdate)// don't let them change ID!!!
	{
		cout<<"Enter  employee ID: ";
		cin>>emp.em_id;
		cin.ignore(100,'\n');
	}

	if (bUpdate)
	{
		cout<<"employee name: "<<emp.em_name<<"\n";
		cout<<"Change employee name?";
		cin>>ch;
	}
	if (ch == 'Y')
	{
		cout<<"Enter  employee name: ";
		cin>>emp.em_name;
		cin.ignore(100,'\n');
	}

	// ... etc.
}

This is just an idea of how you can modify the existing code.

If the person you are copying is who I think it is, be aware that there are many flaws in his code. He would ask questions, get answers, but do a seemingly random copy and paste of the answers, but often in an incorrect way, so the end result, often, is code that doesn't make sense. So if you are trying to learn, realize that the code you are trying to modify was often incorrect in the first place. It might be better to find a different source.

You don't have an option to exit because none is implemented in this code. Look at the menu:

cout<<" 1.Insert new record."<<endl;
         cout<<" 2.Sort record."<<endl;
         cout<<" 3.Delete record."<<endl;
         cout<<" 4.Display record."<<endl;

Add an option to exit, then add a "case" to the switch statement to handle it.

Display record isn't going to work till you read in the records. Reading in the records was one of this person's big problems and it still is with the code you posted. In fact, it looks worse now than when I remember it.

Unless you feel like entering 1000 records from your keyboard, you're going to have to change this. 1000 records is way too many if you are trying to learn from scratch. Have the maximum be 5. Inserting a record is completely incorrect. There should be no loop. The task was to insert a record, meaning one. No loop.

So, if you're a complete newbie and have no idea how the program works, I'd scratch this program and find another one to study. This program can certainly be fixed, and it has potential (many things are correct), but it also has a lot of problems and needs a considerable overhaul.

I am a newbie for c++ since i am just taking up the IT course.
Hmm..should i give up these coding? i thought a record system was the best way to learn up structures and array but later i found that....By the way, where can i find another source about such record system? Thanks for advance ^^''

so..what i can do is
1) declare a new parameter like
void read2( employees record[], int max_1, bool bUpdate=TRUE)

2) then make a function call for read2

Am i right? LOLs..

so..what i can do is
1) declare a new parameter like
void read2( employees record[], int max_1, bool bUpdate=TRUE)

2) then make a function call for read2

Am i right? LOLs..

First, decide whether any of the code you are posting makes anyh sense to you. If it does not, it's time to hit the C++ tutorials. I'd leave "update/edit current record" off for a while. It's a perfectly good idea and a logical extension of this program, but you have enough on your plate just with the existing options (add, delete, display, exit). Figure those out, then add other options.

And as I mentioned to the person whose code you are using as a skeleton, if you have never written a program to insert, delete, and display an array of integers, put the program aside and learn that first before attempting to do it with a struct.

Don't use vectors or anything from STL yet.

http://www.cplusplus.com/reference/stl/

Don't use stuff like "sort" from algorithm.

http://www.cplusplus.com/reference/algorithm/


That's just my opinion. Others will suggest you use them, using the philosophy "why write a sort and why use arrays when there are perfectly good pre-written STL containers and functions already written for you?" There's some wisdom in that, but this code is based on an array, and if you use the pre-written vector and sort stuff, you won't learn about arrays and sorts.

So if you haven't already done so, put this project aside and write a program that inserts, deletes, sorts, and displays a simple integer array. When you get that working, come back to this program.

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

struct employees
{
       int em_id;
       string em_name;
       string position;
       string gender;

};


void read(employees record[], int max_1);

bool IDsort(const  employees &a, const  employees &b)
{ 
     return(a.em_id < b.em_id);

}

char Namesort(const  employees&a, const  employees &b)
{
     return(a.em_name < b.em_name);

}


void Insort(employees record[], int max_2);

void printemployees ( employees record[], int max_3);


int main()
{ 

    const int n= 1000;
    employees record[n];

    int options;




         cout<<"-------------------------------------------------------------------------------"<<endl;
         cout<<"Welcome to Employees Record System"<<endl;
         cout<<"Please insert a number to proceed"<<endl;
         cout<<"-------------------------------------------------------------------------------"<<endl;
         cout<<" 1.Insert new record."<<endl;
         cout<<" 2.Sort record."<<endl;
         cout<<" 3.Delete record."<<endl;
         cout<<" 4.Display record."<<endl;
         cout<<" -------------------------------------------------------------------------------"<<endl;
         cout<<"Selection : ";
         cin>>options;

         switch(options)
         {
             case 0: break;
             case 1: read(record,n);
                     break;
             case 2: Insort(record,n);
                     break;

             case 4: printemployees(record,n);
                     break;
             default: cout<<"Invalid selection"<<endl;
                      break;         

         }                




    system ("pause");
    return 0;
}

 char answer;  
void read( employees record[], int max_1)
{  

     for (int i=0; i<max_1; i++)
     {

         for (int i=0; i<max_1; i++)
         {   
           continued:
             cout<<"employees no. "<<(i+1)<<endl;

             cout<<"Enter  employee ID: ";
             cin>>record[i].em_id;
             cin.ignore(100,'\n');

             cout<<"Enter  employee name: ";
             cin>>record[i].em_name;
             cin.ignore(100,'\n');

             cout<<"Enter  employee position: ";
             cin>>record[i].position;
             cin.ignore(100,'\n');

             cout<<"Enter employee gender: ";
             cin>>record[i].gender;
             cin.ignore(100,'\n');

              cout<<"Do you want to continue?"<<endl;
             cout<<"Select 1 to continue"<<endl;
             cout<<"Select 2 to quit"<<endl;
             cout<<"Select 3 to display"<<endl;
             cin>>answer;

             if (answer == '1') goto continued;
             else if (answer == '3') goto printemployees;
              else main();

             }
         }
}

void Insort(employees record[], int max_2)
{
     int selections=0;
     cout<<"-------------------------------------------------------------------------------"<<endl;
     cout<<"You had selected sorting function"<<endl;
     cout<<"Select a number to proceed"<<endl;
     cout<<"-------------------------------------------------------------------------------"<<endl;
     cout<<"1. Sort by ID"<<endl;
     cout<<"2. Sort by name"<<endl;
     cout<<"-------------------------------------------------------------------------------"<<endl;
     cout<<"Selection: "<<endl;
     cin>>selections;

     switch(selections)
     {
        case 1: std::sort(record,record+max_2,IDsort);
                break;

        case 2: std::sort(record,record+max_2,Namesort);
                break;

     }

}

void printemployees(employees record[], int max_3)
{
     for(int i=0; i<max_3; i++)
     {
             cout<<"employee no. "<<(i+1)<<endl;
             cout<<"employee ID: "<<record[i].em_id<<endl;
             cout<<"employee name: "<<record[i].em_name<<endl;
             cout<<"employeenationality: "<<record[i].position<<endl;
             cout<<"employee gender: "<<record[i].gender<<endl;

     }
}    

If i still stick to these coding..i already tried to loop the thing back to the main() after added if-else statement to the read function..but the option '3' i din't manage to loop it back..what should i do?

And, after i select 2 it loop back to main() then i tried to choose 4 to display it..but can't..whatls wrong with the coding/

can kindly point out?

commented: 15 posts, no code tags - FIGURE IT OUT!!!! -7

I'll repeat what I said in my last post. Does any of the code make sense to you? Do you have any idea what to look for and how to read error messages? If not, take some tutorials and go back to "Hello World". This program is above your skill level at the moment.

Two, code tags. Use them.

[code=C++] // paste code here

[/code]

Three, look at this function. The errors should be obvious. The error messages will clearly state the reasons when you compile the program.

void read( employees record[], int max_1)
{

for (int i=0; i<max_1; i++)
{

for (int i=0; i<max_1; i++)
{
continued:
cout<<"employees no. "<<(i+1)<<endl;

cout<<"Enter employee ID: ";
cin>>record[i].em_id;
cin.ignore(100,'\n');

cout<<"Enter employee name: ";
cin>>record[i].em_name;
cin.ignore(100,'\n');

cout<<"Enter employee position: ";
cin>>record[i].position;
cin.ignore(100,'\n');

cout<<"Enter employee gender: ";
cin>>record[i].gender;
cin.ignore(100,'\n');

cout<<"Do you want to continue?"<<endl;
cout<<"Select 1 to continue"<<endl;
cout<<"Select 2 to quit"<<endl;
cout<<"Select 3 to display"<<endl;
cin>>answer;

if (answer == '1') goto continued;
else if (answer == '3') goto print;
else main();

}
}
}

Do you have a print label? No? Where would you like the program to "go to" when you tell it to "goto print".

Do you have a variable called "answer"?

Do you want a nested loop where both loop control variables are called i and you will never get to the bottom of the loop (you're always short-circuited with a goto or a call to main ())?

There may be an exception to this rule, but NEVER call main () from inside the program (someone will probably think of some time where you might legitimately do so, but it certainly won't be here).

In short, again, take several steps back and look at the program as a whole. You're putting band-aids on some bad ideas to try to make them work instead of doing a complete redesign, which is what is needed.

commented: It might be easier to get blood out of a stone, but not by much ;) +36
commented: I like the conversational way in which your post was written :) +22
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.