Computer Programming lab 11
Lab 11: Strings and Structures
#include<iostream>
#include<string>
using namespace std;
struct student{
string id,name,email,ph_no,depart;
}s[5];
int main()
{
for(int i=0;i<5;i++)
{
cout<<"================================="<<endl;
cout<<"Enter Data Of Student "<<i+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Enter Name : ";
getline(cin,s[i].name);
cout<<"Enter ID : ";
getline(cin,s[i].id);
cout<<"Enter Department : ";
getline(cin,s[i].depart);
cout<<"Enter Email : ";
getline(cin,s[i].email);
cout<<"Enter Phone no : ";
getline(cin,s[i].ph_no);
}
for(int i=0;i<5;i++)
{
cout<<"================================="<<endl;
cout<<"Data Of Student "<<i+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Name : "<<s[i].name<<endl;
cout<<"ID : "<<s[i].id<<endl;
cout<<"Department : "<<s[i].depart<<endl;
cout<<"Email : "<<s[i].email<<endl;
cout<<"Phone no : "<<s[i].ph_no<<endl;
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
void display(int);
struct record{
string pro_name;
int model;
double price;
}r[10];
int main(){
int i=0;
do{
cout<<"================================="<<endl;
cout<<"Enter Data Of Product "<<i+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Or Type quit to terminate "<<endl;
cout<<"Enter Name : ";
cin>>r[i].pro_name;
if(r[i].pro_name=="quit")
{
break;
}
cout<<"Enter Model : ";
cin>>r[i].model;
cout<<"Enter Price : ";
cin>>r[i].price;
i++;
}
while(i<10);
display(i);
}
void display(int a)
{
for(int j=0;j<a;j++)
{
cout<<"================================="<<endl;
cout<<"Data Of Product "<<j+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Enter Name : "<<r[j].pro_name<<endl;
cout<<"Enter Model : "<<r[j].model<<endl;
cout<<"Price : "<<r[j].price<<endl;
}
}
#include<iostream>
#include<string.h>
using namespace std;
void emp_salary();
void display();
struct employe{
string name;
long int salary;
double house_all,medi_all,gross_salary,tax,net_salary;
int employee_id;
}e;
int main()
{
cout<<"Enter Employee ID : ";
cin>>e.employee_id;
cout<<"Enter Employee Name : ";
cin>>e.name;
cout<<"Enter Salary : ";
cin>>e.salary;
emp_salary();
display();
}
void emp_salary()
{
e.house_all=e.salary/100*10;
e.medi_all=e.salary/100*5;
e.gross_salary=e.salary+e.house_all+e.medi_all;
e.tax=e.salary/100*4;
e.net_salary=e.gross_salary-e.tax;
}
void display()
{
cout<<"House Allowence : "<<e.house_all<<endl;
cout<<"Medical Allowence : "<<e.medi_all<<endl;
cout<<"Gross Salary : "<<e.gross_salary<<endl;
cout<<"Tax : "<<e.tax<<endl;
cout<<"Net Salary : "<<e.net_salary<<endl;
}
Exercise 2
Write a C++
program that maintain record of students. Student contains following details:
- ID
- Name
- Department
- Email
- Phone no
Create a
structure of student. Ask user to enter record for 5 students. Store those details
in variables of students type. Print those records on screen.
Source Code:#include<iostream>
#include<string>
using namespace std;
struct student{
string id,name,email,ph_no,depart;
}s[5];
int main()
{
for(int i=0;i<5;i++)
{
cout<<"================================="<<endl;
cout<<"Enter Data Of Student "<<i+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Enter Name : ";
getline(cin,s[i].name);
cout<<"Enter ID : ";
getline(cin,s[i].id);
cout<<"Enter Department : ";
getline(cin,s[i].depart);
cout<<"Enter Email : ";
getline(cin,s[i].email);
cout<<"Enter Phone no : ";
getline(cin,s[i].ph_no);
}
for(int i=0;i<5;i++)
{
cout<<"================================="<<endl;
cout<<"Data Of Student "<<i+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Name : "<<s[i].name<<endl;
cout<<"ID : "<<s[i].id<<endl;
cout<<"Department : "<<s[i].depart<<endl;
cout<<"Email : "<<s[i].email<<endl;
cout<<"Phone no : "<<s[i].ph_no<<endl;
}
return 0;
}
Exercise 3
Write a C++ program
to do the following:
-
Set up a structure/record to store products; each
product has a name, a model number and a price.
-
Choose appropriate types for these fields.
-
Your program should contain a loop which allows entry
via the keyboard of up to 10 products, to be stored in an array.
-
The loop can be stopped by entry of "quit"
for the product name
-
Your program should include a function to display all
the products details after the entry loop has finished.
Source Code:#include<iostream>
#include<string>
using namespace std;
void display(int);
struct record{
string pro_name;
int model;
double price;
}r[10];
int main(){
int i=0;
do{
cout<<"================================="<<endl;
cout<<"Enter Data Of Product "<<i+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Or Type quit to terminate "<<endl;
cout<<"Enter Name : ";
cin>>r[i].pro_name;
if(r[i].pro_name=="quit")
{
break;
}
cout<<"Enter Model : ";
cin>>r[i].model;
cout<<"Enter Price : ";
cin>>r[i].price;
i++;
}
while(i<10);
display(i);
}
void display(int a)
{
for(int j=0;j<a;j++)
{
cout<<"================================="<<endl;
cout<<"Data Of Product "<<j+1<<" : "<<endl;
cout<<"================================="<<endl;
cout<<"Enter Name : "<<r[j].pro_name<<endl;
cout<<"Enter Model : "<<r[j].model<<endl;
cout<<"Price : "<<r[j].price<<endl;
}
}
Exercise 3
Write a C++ program
to do the following:
-
Set up a structure/record to store products; each
product has a name, a model number and a price.
-
Choose appropriate types for these fields.
-
Your program should contain a loop which allows entry
via the keyboard of up to 10 products, to be stored in an array.
-
The loop can be stopped by entry of "quit"
for the product name
-
Your program should include a function to display all
the products details after the entry loop has finished.
Exercise 4
Write a
C++ program that compute Net
Salary of Employee. Program contains two user defined functions empSalary() and display().
·
Create
a structure of Employee that contains following data members:
o
EmployeeNumber,
Name, BasicSalary, HouseAllowance, MedicalAllowance,Tax, GrossPay and NetSalary
·
Employeenumber,name
and basicsalary must be taken input from the user.
·
empSalary() compute salary with given criteria
o
HouseAllowence
= 10% of BasicSalary
o
Medical
Allowence = 5% of Basic Salary
o
Tax
= 4 % of Basic Salary
o
GrossSalary
= Basic+HouseAllowence+MedicalAllowence
o
NetSalary
= GrossSalary – Tax
·
display() for displaying details of Empolyee
Source Code:#include<iostream>
#include<string.h>
using namespace std;
void emp_salary();
void display();
struct employe{
string name;
long int salary;
double house_all,medi_all,gross_salary,tax,net_salary;
int employee_id;
}e;
int main()
{
cout<<"Enter Employee ID : ";
cin>>e.employee_id;
cout<<"Enter Employee Name : ";
cin>>e.name;
cout<<"Enter Salary : ";
cin>>e.salary;
emp_salary();
display();
}
void emp_salary()
{
e.house_all=e.salary/100*10;
e.medi_all=e.salary/100*5;
e.gross_salary=e.salary+e.house_all+e.medi_all;
e.tax=e.salary/100*4;
e.net_salary=e.gross_salary-e.tax;
}
void display()
{
cout<<"House Allowence : "<<e.house_all<<endl;
cout<<"Medical Allowence : "<<e.medi_all<<endl;
cout<<"Gross Salary : "<<e.gross_salary<<endl;
cout<<"Tax : "<<e.tax<<endl;
cout<<"Net Salary : "<<e.net_salary<<endl;
}
Comments
Post a Comment