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++ > Data Structures Code Examples

Employee Management Database System

/* Employee Management Database System */ #include<fstream.h> #include<dos.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<iomanip.h> #include<graphics.h> class group { private: struct person { char flag; char empcode[5]; char name[40]; int age; float sal; int day; int month; int year; }p; fstream file; public: group(); void addrec(); void listrec(); void modirec(); void delrec(); void recallrec(); void packrec(); void exit(); }; void main() { char choice,v; group g; do { clrscr(); struct dosdate_t d; _dos_getdate(&d); // p.day=d.day; // p.month=d.month; // p.year=d.year; // _dos_getdate(&d); gotoxy(12,5); textcolor(6); cprintf("Todays date:"); printf("%d",d.day); cout<<"/"; printf("%d",d.month); cout<<"/"; cout<<d.year; gotoxy(12,9); cout<<"Srknec.corporation pvt.ltd"; gotoxy(12,12); cout<<"1.Add record"; gotoxy(12,15); cout<<"2.List Record"; gotoxy(12,17); cout<<"3.Modify record"; gotoxy(12,19); cout<<"4.Delete record"; gotoxy(12,21); cout<<"5.Recall record"; gotoxy(12,23); cout<<"6.Pack record"; gotoxy(12,25); cout<<"0.Exit"; gotoxy(12,28); cout<<"Your choice"<<" "; cin>>choice; clrscr(); switch(choice) { case '1': g.addrec(); break; case'2': g.listrec(); break; case'3': g.modirec(); break; case'4': g.delrec(); break; case'5': g.recallrec(); break; case'6': g.packrec(); break; case'0': g.exit(); exit(1); } } while(choice!=0); } void group::group() { file.open("Emp.dat",ios::binary|ios::in|ios::out); p.flag=' '; if(!file) { cout<<endl<<"Unable to open file"; exit(); } } void group::addrec() { char ch; file.seekp(0L,ios::end); struct dosdate_t d; _dos_getdate(&d); p.day=d.day; p.month=d.month; p.year=d.year; cout<<"Make shure that no employee has the same code"<<endl; do { cout<<endl<<"Enter employee code, name, age & salary :"<<endl<<endl; cout<<"OR"<<endl<<endl; cout<<"To return to the Main menu press'm' key else press'a':"; cin>>ch; if(ch=='m'||ch=='M') { main(); } cin>>p.empcode>>p.name>>p.age>>p.sal; p.flag=' '; file.write((char*)&p,sizeof(p)); cout<<"Add another record ?(y/n) :"; cin>>ch; } while(ch=='y'||ch=='Y'); } void group::listrec() { int j=0,a; file.seekg(0L,ios::beg); cout<<"List of records present are as under>>>"<<endl<<endl; cout<<" "<<"|CODE|"<<" "<<"|NAME|"<<" "<<"|AGE|"<<" "<<"|SALARY|"<<" "<<"|DATED|"<<endl; //Donot touch Above Line Any Way it is set to give Some Nice Look while(file.read((char*)&p,sizeof(p))) { if(p.flag!='*') cout<<endl<<"Record#"<<" "<<j++<<setw(6)<<p.empcode<<setw(20)<<p.name<<setw(4)<<p.age<<setw(9)<<p.sal<<" "<<p.day<<"/"<<p.month<<"/"<<p.year<<endl; } file.clear(); if(j==0) { gotoxy(10,10); cout<<"No record exit"; gotoxy(10,11); cout<<"Press any key..."; getch(); } else { cout<<endl<<"Press any key..."; getch(); } } void group::modirec() { char code[5]; int count=0; long int pos; cout<<"Enter employee code whose record is to be modified :"; cin>>code; file.seekg(0L,ios::beg); while(file.read((char*)&p,sizeof(p))) { if(strcmp(p.empcode,code)==0) { cout<<endl<<"Enter new record "<<endl; cout<<endl<<"Enter employee name,age & salary :"<<endl; cin>>p.name>>p.age>>p.sal; struct dosdate_t d; _dos_getdate(&d); p.day=d.day; p.month=d.month; p.year=d.year; p.flag=' '; pos=count*sizeof(p); file.seekp(pos,ios::beg); file.write((char*)&p,sizeof(p)); return; } count++; } cout<<endl<<"No employee in file with code= "<<code; getch(); file.clear(); } void group::delrec() { char code[5]; long int pos; int count=0; cout<<"Enter employee code to be deleted :"; cin>>code; file.seekg(0L,ios::beg); while(file.read((char*)&p,sizeof(p))) { if (strcmp(p.empcode,code)==0) { p.flag='*'; pos=count*sizeof(p); file.seekp(pos,ios::beg); file.write((char*)&p,sizeof(p)); return; } count++; } cout<<endl<<"No employee in file with code="<<code; cout<<endl<<"Press any key..."; getch(); file.clear(); } void group::recallrec() { char code[5]; long int pos; int count=0; cout<<"Enter employee code to be recalled :"; cin>>code; file.seekg(0L,ios::beg); while(file.read((char*)&p,sizeof(p))) { if(strcmp(p.empcode,code)==0) { p.flag=' '; pos=count*sizeof(p); file.seekp(pos,ios::beg); file.write((char*)&p,sizeof(p)); return; } count++; } cout<<endl<<"No employee in the file with code="<<code; cout<<endl<<"Press any key..."; getch(); file.clear(); } void group::packrec() { ofstream outfile; outfile.open("TEMP",ios::out); file.seekg(0,ios::beg); while(file.read((char*)&p,sizeof(p))) { if(p.flag!='*') outfile.write((char*)&p,sizeof(p)); } outfile.close(); file.close(); remove("Emp.DAT"); rename("TEMP","EMP.DAT"); file.open("EMP.DAT",ios::binary|ios::in|ios::out|ios::nocreate); gotoxy(10,10); cout<<"Marked records to be deleted are all removed"; gotoxy(10,11); cout<<"press any key...."; getch(); } void group::exit() { file.close(); }

Rename file. Changes the name of the file or directory specified by oldname to newname. This is an operation performed directly on a file; No streams are involved in the operation. If oldname and newname specify different paths and this is supported by the system, the file is moved to the new location. If newname names an existing file, the function may either fail or override the existing file, depending on the specific system and library implementation. Proper file access shall be available.

Set position in input sequence. Sets the position of the next character to be extracted from the input stream. Sets the position of the next character to be extracted from the input stream. seekg() is a function in the iostream library (part of the standard library) that allows you to seek to an arbitrary position in a file. It is used in file handling to sets the position of the next character to be extracted from the input stream from a given file. Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it calls either pubseekpos (1) or pubseekoff (2) on its associated stream buffer object (if any). Finally, it destroys the sentry object before returning. Calling this function does not alter the value returned by gcount.

#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.

The sizeof() is an operator that evaluates the size of data type, constants, variable. It is a compile-time operator as it returns the size of any variable or a constant at the compilation time. The size, which is calculated by the sizeof() operator, is the amount of RAM occupied in the computer. The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. The data_type can be the data type of the data, variables, constants, unions, structures, or any other user-defined data type.

The remove() function in C++ deletes a specified file. Deletes the file whose name is specified in filename. The remove() function takes a single argument filename and returns an integer value. It deletes the file pointed by the parameter. This is an operation performed directly on a file identified by its filename; No streams are involved in the operation. Proper file access shall be available. Incase the file to be deleted is opened by a process, the behaviour of remove() function is implementation-defined. It is defined in <cstdio> header file.

Check whether eofbit is set. Returns true if the eofbit error state flag is set for the stream. This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream. Note that the value returned by this function depends on the last operation performed on the stream (and not on the next). Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.

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.

In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively. To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is: • ofstream: This data type represents the output file stream and is used to create files and to write information to files. • ifstream: This data type represents the input file stream and is used to read information from files. • fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

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.

Set position in output sequence. Sets the position where the next character is to be inserted into the output stream. Internally, the function accesses the output sequence by first constructing a sentry object without evaluating it. Then, if member fail returns true, the function returns. Otherwise, it calls either pubseekpos (1) or pubseekoff (2) on its associated stream buffer object (if any). Finally, it destroys the sentry object before returning. Notice that the function will work even if the eofbit flag is set before the call, but it will not modify it.

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.

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.

Positions cursor in text window. The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. If the coordinates are in any way invalid the call to gotoxy is ignored. Neither argument to gotoxy can be zero.

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++.

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.

Read block of data. Extracts n characters from the stream and stores them in the array pointed to by s. This function simply copies a block of data, without checking its contents nor appending a null character at the end. If the input sequence runs out of characters to extract (i.e., the end-of-file is reached) before n characters have been successfully read, the array pointed to by s contains all the characters read until that point, and both the eofbit and failbit flags are set for the stream. Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it extracts characters from its associated stream buffer object as if calling its member functions sbumpc or sgetc, and finally destroys the sentry object before returning.

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.

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.

Use the textcolor function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral. Now, if you want your text to blink then while calling the textcolor() function pass the color and also say BLINK. This will like this: textcolor(BLUE+BLINK).

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.

Open file. Opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails. The file association of a stream is kept by its internal stream buffer: Internally, the function calls rdbuf()->open(filename,mode|ios_base::out). The function clears the stream's state flags on success (setting them to goodbit). In case of failure, failbit is set.

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

Set error state flags. Sets a new value for the stream's internal error state flags. The clear() method of ios class in C++ is used to change the current state of the specified flag by setting it. Hence this function changes the internal state of this stream. The current value of the flags is overwritten: All bits are replaced by those in state; If state is goodbit (which is zero) all error flags are cleared. In the case that no stream buffer is associated with the stream when this function is called, the badbit flag is automatically set (no matter the value for that bit passed in argument state). Note that changing the state may throw an exception, depending on the latest settings passed to member exceptions.

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

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.

Write block of data. Inserts the first n characters of the array pointed by s into the stream. This function simply copies a block of data, without checking its contents: The array may contain null characters, which are also copied without stopping the copying process. Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it inserts character into its associated stream buffer object as if calling its member function sputc until n characters have been written or until an insertion fails (in this case it sets the badbit flag). Finally, it destroys the sentry object before returning.

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.


Getting the No of processes and Burst Time. "First come First served" Algorithm. Shortest job first algorithm with preemption. Shortest job First Algorithm with NonPreemption. Cpu

This program decodes any message encoded by the technique of traditional playfair cipher. "The Playfair Cipher" is a 'Manual Symmetric' Encryption Technique and was the first literal

This takes an integer input from the user and stores it in 'variable n'. Then the while loop is iterated until n != 0 is false. In each iteration, the remainder when the value of n is divided