Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Beginners Lab Assignments Code Examples

Project - Employees Management System

/* Project - Employees Management System */ #include<iostream.h> #include<string.h> #include<iomanip.h> #include<dos.h> #include<conio.h> #include<stdio.h> #define max 20 struct employee { char name[20]; long int code; char designation[20]; int exp; int age; }; int num; employee emp[max],tempemp[max],sortemp[max],sortemp1[max]; void main() { clrscr(); void build(); void list(); void insert(); void deletes(); void edit(); void search(); void sort(); char option; void menu(); menu(); while((option=cin.get())!='q') { switch(option) { case 'b': build(); break; case 'l': list(); break; case 'i': insert(); break; case 'd': deletes(); break; case 'e': edit(); break; case 's': search(); break; case 'n': sort(); break; } menu(); } } void menu() { clrscr(); highvideo(); cout<<" "; cprintf("*****WelCome To Employee Data Centre***** "); normvideo(); cout<<endl; cout<<" "; cout<<"Press b---->Built The Employee Table "; cout<<" "; cout<<"Press l---->List The Employee Table "; cout<<" "; cout<<"Press i---->Insert New Entry "; cout<<" "; cout<<"Press d---->Delete An Entry "; cout<<" "; cout<<"Press e---->Edit An Entry "; cout<<" "; cout<<"Press s---->Search Arecord "; cout<<" "; cout<<"Press n---->Sort The Table "; cout<<" "; cout<<"Press q---------->QUIT "; cout<<" "; cout<<"Option Please ----->"; } void build() { clrscr(); highvideo(); cprintf("Build The Table "); cout<<endl; normvideo(); cout<<"maximum number of entries ----- > 20"<<endl; cout<<"how many do you want ----->"; cin>>num; cout<<"Enter The Following Items "; for(int i=0;i<=num-1;i++) { cout<<" Name "; cin>>emp[i].name; cout<<"Code "; cin>>emp[i].code; cout<<"Designation "; cin>>emp[i].designation; cout<<"Years of Experience "; cin>>emp[i].exp; cout<<"Age "; cin>>emp[i].age; } cout<<"going to main menu"; delay(500); } void list() { clrscr(); highvideo(); cprintf(" ********List The Table********"); cout<<endl; normvideo(); cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; for(int i=0;i<=num-1;i++) { cout<<setw(13)<<emp[i].name; cout<<setw(6)<<emp[i].code; cout<<setw(15)<<emp[i].designation; cout<<setw(10)<<emp[i].exp; cout<<setw(15)<<emp[i].age; cout<<endl; } cout<<"going to main menu"; getch(); } void insert() { clrscr(); int i=num; num+=1; highvideo(); cprintf("Insert New Record "); cout<<endl; normvideo(); cout<<"Enter The Following Items "; cout<<"Name "; cin>>emp[i].name; cout<<"Code "; cin>>emp[i].code; cout<<"Designation "; cin>>emp[i].designation; cout<<"Years of Experience "; cin>>emp[i].exp; cout<<"Age "; cin>>emp[i].age; cout<<endl<<endl; cout<<"going to main menu"; delay(500); } void deletes() { clrscr(); highvideo(); int code; int check; cprintf("Delete An Entry "); normvideo(); cout<<endl; cout<<"Enter An JobCode To Delete That Entry "; cin>>code; int i; for(i=0;i<=num-1;i++) { if(emp[i].code==code) { check=i; } } for(i=0;i<=num-1;i++) { if(i==check) { continue; } else { if(i>check) { tempemp[i-1]=emp[i]; } else { tempemp[i]=emp[i]; } } } num--; for(i=0;i<=num-1;i++) { emp[i]=tempemp[i]; } } void edit() { clrscr(); int jobcode; highvideo(); cprintf(" Edit An Entry "); cout<<endl; cout<<endl; int i; void editmenu(); void editname(int); void editcode(int); void editdes(int); void editexp(int); void editage(int); char option; normvideo(); cout<<"Enter An jobcode To Edit An Entry---- "; cin>>jobcode; editmenu(); for(i=0;i<=num-1;i++) { if(emp[i].code==jobcode) { while((option=cin.get())!='q') { switch(option) { case 'n': editname(i); break; case 'c': editcode(i); break; case 'd': editdes(i); break; case 'e': editexp(i); break; case 'a': editage(i); break; } editmenu(); } } } } void editmenu() { clrscr(); cout<<" What Do You Want To edit "; cout<<" n--------->Name "; cout<<" c--------->Code "; cout<<" d--------->Designation "; cout<<" e--------->Experience "; cout<<" a--------->Age "; cout<<" q----->QUIT "; cout<<" Options Please ---->>> "; } void editname(int i) { cout<<"Enter New Name-----> "; cin>>emp[i].name; } void editcode(int i) { cout<<"Enter New Job Code-----> "; cin>>emp[i].code; } void editdes(int i) { cout<<"enter new designation-----> "; cin>>emp[i].designation; } void editexp(int i) { cout<<"Enter new Years of Experience "; cin>>emp[i].exp; } void editage(int i) { cout<<"Enter new Age "; cin>>emp[i].age; } void search() { clrscr(); highvideo(); cprintf("Welcome To Search Of Employee Database "); normvideo(); cout<<endl; cout<<endl; int jobcode; cout<<"You Can Search Only By Jobcode Of An Employee "; cout<<"Enter Code Of An Employee "; cin>>jobcode; for(int i=0;i<=num-1;i++) { if(emp[i].code==jobcode) { cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; cout<<setw(13)<<emp[i].name; cout<<setw(6)<<emp[i].code; cout<<setw(15)<<emp[i].designation; cout<<setw(10)<<emp[i].exp; cout<<setw(15)<<emp[i].age; cout<<endl; } } cout<<"going to main menu"; getch(); } void sort() { clrscr(); highvideo(); cprintf("Sort The Databse By JobCode "); normvideo(); void sortmenu(); void sortname(); void sortcode(); void sortdes(); void sortexp(); char option; void sortage(); cout<<endl; cout<<endl; sortmenu(); while((option=cin.get())!='q') { switch(option) { case 'n': sortname(); break; case 'c': sortcode(); break; case 'd': sortdes(); break; case 'e': sortexp(); break; case 'a': sortage(); break; } sortmenu(); } } void sortmenu() { clrscr(); cout<<" What Do You Want To edit "; cout<<" n--------->Name "; cout<<" c--------->Code "; cout<<" d--------->Designation "; cout<<" e--------->Experience "; cout<<" a--------->Age "; cout<<" q----->QUIT "; cout<<" Options Please ---->>> "; } void sortname() { clrscr(); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(strcmp(sortemp1[i].name,sortemp1[j].name)<=0) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); } } void sortcode() { clrscr(); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(sortemp1[i].code<sortemp1[j].code) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); } } void sortdes() { clrscr(); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(strcmp(sortemp1[i].designation,sortemp1[j].designation)<=0) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); } } void sortage() { clrscr(); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(sortemp1[i].age<sortemp1[j].age) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); } } void sortexp() { clrscr(); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(sortemp1[i].exp<sortemp1[j].exp) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Years(EXP) Age "; cout<<" ------------------------------------------------------ "; for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); } }

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Get type name. name() returns a null-terminated character sequence that may identify the type. The particular representation pointed by the returned value is implementation-defined, and may or may not be different for different types. This function does not accept any parameter.

Compute exponential function. Returns the base-e exponential function of x, which is e raised to the power x: ex. This function is defined in <cmath> header file. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations. This function is also overloaded in <complex> and <valarray> (see complex exp and valarray exp). The function can take any value i.e, positive, negative or zero in its parameter and returns result in int, double or float or long double. Function returns exponential value of x.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

Get characters. Extracts characters from the stream, as unformatted input. The get() function is used to read a character(at a time) from a file. The classes istream and ostream define two member functions get(), put() respectively to handle the single character input/output operations. There are two types of get() functions. Both get(char *) and get(void) prototype can be used to fetch a character including the blank space,tab and newline character. The get(char *) version assigns the input character to its argument and the get(void) version returns the input character. Since these functions are members of input/output Stream classes, these must be invoked using appropriate objects.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

Compare two strings. Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll. The strcmp() function in C++ compares two null-terminating strings (C-strings). The comparison is done lexicographically. It is defined in the cstring header file.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

Search range for subsequence. Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2). This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end. The function shall not modify any of its arguments.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.

Continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration. The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

Set field width. Sets the field width to be used on output operations. The C++ function std::setw behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). It is used to sets the field width to be used on output operations. This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the field width is to be set. This function returns an object of unspecified type. The setw function should only be used as a stream manipulator.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value




The second power was described in terms of the area of a square, as in the above formula. It led to the use of the term square to mean raising to the "second power". Thus the area





To find the "Largest Element", the first two elements of array are checked and largest of these two element is placed in arr[0]. Then, the first and third elements are checked and