+ Link For Assignments, GDBs & Online Quizzes Solution |
+ Link For Past Papers, Solved MCQs, Short Notes & More |
CS201 Assignment No 03 Fall 2019 Solution & Discussion
Tags:
+ http://bit.ly/vucodes (Link for Assignments, GDBs & Online Quizzes Solution)
+ http://bit.ly/papersvu (Link for Past Papers, Solved MCQs, Short Notes & More)
+ Click Here to Search (Looking For something at vustudents.ning.com?) + Click Here To Join (Our facebook study Group)CS201 Assignment No 3 Solution Fall 2019 – 2020
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
voidaddemployee();
voidshowemployee();
class employee {
intemployee_id;
stringemployee_name;
intemployee_salary;
public:
employee(){
}
voidsetid(int id){
employee_id=id;
}
voidsetname(string name){
employee_name=name;
}
voidsetsalary(int salary){
employee_salary=salary;
}
intgetid(){
return this->employee_id;
}
stringgetname(){
return this->employee_name;
}
intgetsalary(){
return this->employee_salary;
}
};
employeeanEmployee;
voidaddemployee(){
int id;
cout"enter employee ID: ";
cin>>id;
anEmployee.setid(id);
cout"enter employee Name: ";
string name;
cin>>name;
anEmployee.setname(name);
intsalry;
cout"enter employee Salary: ";
cin>>salry;
anEmployee.setsalary(salry);
ofstreamempfile("employee.txt",ios::binary|ios::app);
if(!empfile) {
cout "Cannot open file!" endl;
return ;
}
empfile.write((char*)&anEmployee, sizeof(employee));
//coutanEmployee.getid()" "anEmployee.getname()" """anEmployee.getsalary();
empfile.close();
if(!empfile.good()) {
cout "Error occurred at writing time!" endl;
return ;
}
cout "Employee Recored added Sucessfully!" endl;
}
voiddisplayRecord() {
// employee anEmployee;
ifstream file("employee.txt",ios::binary);
if(!file) {
cout"Error in opening file.\n";
return;
} else {
cout"ID"" ""Name""\t""Salary"endl;
cout"======================================================="endl;
while(file.read((char*)&anEmployee,sizeof(employee))) {
coutanEmployee.getid()" "anEmployee.getname()"\t"anEmployee.getsalary();
coutendl;
}
file.close();
}
}
// function to show employee data
voidupdateSalary() {
fstream file("employee.txt",ios::binary|ios::in|ios::out);
if(!file) {
cout"Error in opening file.\n";
return;
}
inteCode,sHike;
cout"Enter employee code\n";
cin>>eCode;
cout"Salary hike? ";
cin>>sHike;
file.seekg(sizeof(anEmployee)*(eCode-1),ios::beg);
file.read((char*)&anEmployee,sizeof(employee));
anEmployee.setsalary(sHike+anEmployee.getsalary());
file.seekp(sizeof(anEmployee)*(eCode-1),ios::beg);
file.write((char*)&anEmployee,sizeof(employee));
cout"Salary updated successfully.\n";
}
main()
{
ifstream file("employee.txt");
if(file) {
file.close();
remove("employee.txt");
}
intprogramOption;
while(true){
cout"\n\n\n";
cout"Please select the option Below,.\n";
cout"Please enter 1 To ADD AN EMPLOYEE.\n";
cout"Please enter 2 To DISPLAY FILE DATA..\n";
cout"Please enter 3 To INCREASE EMPLOYEE SALARY..\n";
cout"Please enter 4 To Exit Program..\n";
cout"==================================================================\n";
cout"Please enter program option :";
cin>>programOption;
switch(programOption){
case 1 :
cout"ADD AN EMPLOYEE DATA.\n";
addemployee();
break;
case 2 :
cout"Showing Employee Data.\n";
displayRecord();
break;
case 3 :
cout"Update salary .\n";
updateSalary();
break;
case 4 :
cout"Shuting Down the Program..... .\n";
exit(1);
break;
}
}
}
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
class Employee{
private:
int code;
char name[20];
float salary;
public:
void read();
void display();
int getEmpCode() { return code;}
int getSalary() { return salary;}
void updateSalary(float s) { salary=s;}
};
void Employee::read(){
cout"Enter employee code: ";
cin>>code;
cout"Enter name: ";
cin.ignore(1);
cin.getline(name,20);
cout"Enter salary: ";
cin>>salary;
}
void Employee::display()
{
coutcode" "name"\t"salaryendl;
}
fstream file;
void deleteExistingFile(){
remove("EMPLOYEE.DAT");
}
void appendToFille(){
Employee x;
x.read();
file.open("EMPLOYEE.DAT",ios::binary|ios::app);
if(!file){
cout"ERROR IN CREATING FILE\n";
return;
}
file.write((char*)&x,sizeof(x));
file.close();
cout"Record added sucessfully.\n";
}
void displayAll(){
Employee x;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x)))
if(x.getSalary()>=10000 && x.getSalary()<=20000)
x.display();
}
file.close();
}
void searchForRecord(){
Employee x;
int c;
int isFound=0;
cout"Enter employee code: ";
cin>>c;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()==c){
cout"RECORD FOUND\n";
x.display();
isFound=1;
break;
}
}
}
if(isFound==0){
cout"Record not found!!!\n";
}
file.close();
}
void increaseSalary(){
Employee x;
int c;
int isFound=0;
float sal;
cout"enter employee code \n";
cin>>c;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()==c){
cout"Salary hike? ";
cin>>sal;
x.updateSalary(x.getSalary()+sal);
isFound=1;
break;
}
}
}
if(isFound==0){
cout"Record not found!!!\n";
}
file.close();
cout"Salary updated successfully."endl;
}
void insertRecord(){
Employee x;
Employee newEmp;
newEmp.read();
fstream fin;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
fin.open("TEMP.DAT",ios::binary|ios::out);
if(!file){
cout"Error in opening EMPLOYEE.DAT file!!!\n";
return;
}
if(!fin){
cout"Error in opening TEMP.DAT file!!!\n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()>newEmp.getEmpCode()){
fin.write((char*)&newEmp, sizeof(newEmp));
}
fin.write((char*)&x, sizeof(x));
}
}
fin.close();
file.close();
rename("TEMP.DAT","EMPLOYEE.DAT");
remove("TEMP.DAT");
cout"Record inserted successfully."endl;
}
int main()
{
char ch;
deleteExistingFile();
do{
int n;
cout"ENTER CHOICE\n""1.ADD AN EMPLOYEE\n""2.DISPLAY\n""3.SEARCH\n""4.INCREASE SALARY\n""5.INSERT RECORD\n";
cout"Make a choice: ";
cin>>n;
switch(n){
case 1:
appendToFille();
break;
case 2 :
displayAll();
break;
case 3:
searchForRecord();
break;
case 4:
increaseSalary();
break;
case 5:
insertRecord();
break;
default :
cout"Invalid Choice\n";
}
cout"Do you want to continue ? : ";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}
CS201 Solution Assignment No 03 Fall 2019
below link
CS201_Assignmnet_03_Solution_Fall_2019_vustudents.ning.com.cpp
© 2021 Created by + M.Tariq Malik.
Powered by
Promote Us | Report an Issue | Privacy Policy | Terms of Service
We have been working very hard since 2009 to facilitate in learning Read More. We can't keep up without your support. Donate.
We are user-generated contents site. All product, videos, pictures & others contents on site don't seem to be beneath our Copyrights & belong to their respected owners & freely available on public domains. We believe in Our Policy & do according to them. If Any content is offensive in your Copyrights then please email at m.tariqmalik@gmail.com Page with copyright detail & We will happy to remove it immediately.
Management: Admins ::: Moderators
Awards Badges List | Moderators Group
All Members | Featured Members | Top Reputation Members | Angels Members | Intellectual Members | Criteria for Selection
Become a Team Member | Safety Guidelines for New | Site FAQ & Rules | Safety Matters | Online Safety | Rules For Blog Post