You are on page 1of 12

22F-3623

OOP_LAB-6
BISAM AHMAD

Task-1:

#include<iostream>
#include<string>
using namespace std;

struct library
{
char title[20];
int identifier;
char author[20];
char genere[20];
int found;
bool flag1 = true;
bool flag2 = true;
bool flag3 = true;

void inputs(library books[3])


{
for (int i = 0; i < 2; i++)
{
cout << "Enter identifier = ";
cin >> books[i].identifier;
cin.ignore();
cout << "Enter book title = ";
cin.get(books[i].title, 20);
cin.ignore();
cout << "Enter author = ";
cin.get(books[i].author, 20);
cin.ignore();
cout << "Enter genere = ";
cin.get(books[i].genere, 20);
cin.ignore();
}
}

void searching(library books[3])


{
char search;
cout << "Enter i for search by identifier or t for title or a for author or
g for genere = ";
cin >> search;
switch (search)
{
case 'i':
{
cout << "Enter identifier = ";
cin >> books[2].identifier;
if (books[2].identifier == books[0].identifier)
{
books[0].found = 0;
}
else if (books[2].identifier == books[1].identifier)
{
books[0].found = 1;
}
else
{
cout << "Not found" << endl;
}
break;
}
case 't':
{
cout << "Enter title = ";
cin >> books[2].title;
for (int i = 0; books[2].title < '\0'; i++)
{
if (books[2].title[i] != books[0].title[i])
{
books[0].flag1 = false;
break;
}
}
if (!books[0].flag1)
{
for (int i = 0; books[2].title < '\0'; i++)
{
if (books[2].title[i] != books[1].title[i])
{
books[0].flag2 = false;
break;
}
}
}
if (books[0].flag1)
{
books[0].found = 0;
}
else if (books[0].flag2)
{
books[0].found = 1;
}
else
{
cout << "Not found" << endl;
}
break;
}
case 'a':
{
cout << "Enter author = ";
cin >> books[2].author;
for (int i = 0; books[2].author < '\0'; i++)
{
if (books[2].author[i] != books[0].author[i])
{
books[0].flag1 = false;
break;
}
}
if (!books[0].flag1)
{
for (int i = 0; books[2].author < '\0'; i++)
{
if (books[2].author[i] != books[1].author[i])
{
books[0].flag2 = false;
break;
}
}
}
if (books[0].flag1)
{
books[0].found = 0;
}
else if (books[0].flag2)
{
books[0].found = 1;
}
else
{
cout << "Not found" << endl;
}
break;
}
case 'g':
{
cout << "Enter genere = ";
cin >> books[2].genere;
for (int i = 0; books[2].genere < '\0'; i++)
{
if (books[2].genere[i] != books[0].genere[i])
{
books[0].flag1 = false;
break;
}
}
if (!books[0].flag1)
{
for (int i = 0; books[2].genere < '\0'; i++)
{
if (books[2].genere[i] != books[1].genere[i])
{
books[0].flag2 = false;
break;
}
}
}
if (books[0].flag1)
{
books[0].found = 0;
}
else if (books[0].flag2)
{
books[0].found = 1;
}
else
{
cout << "Not found" << endl;
}
break;
}
default:
{
cout << "Invalid input" << endl;
books[0].flag3 = false;
break;
}
}
}
};
int main()
{
library books[3];
char choice;
books[0].inputs(books);
cout << endl;
cout << "If you want to search a book (y\n) = ";
cin >> choice;
if (choice == 'y')
{
books[0].searching(books);
if (books[0].flag3)
{
cout << endl;
cout << "Required book" << endl;
cout << " Author = " << books[books[0].found].author << endl;;
cout << " Genere = " << books[books[0].found].genere << endl;
cout << " Identifier = " << books[books[0].found].identifier <<
endl;
cout << " Title = " << books[books[0].found].title << endl;
}
}
system("pause");
return 0;
}

Output:
Task-2:

#include <iostream>
#include <string>
using namespace std;

struct player
{
char name[20];
int home_runs;
int hits;

void input(player players[2])


{
for (int i = 0; i < 2; i++)
{
cout << "Enter data for player " << i + 1 << endl;
cin.ignore();
cout << "Name = ";
cin.get(players[i].name, 20);
cout << "Number of home runs = ";
cin >> players[i].home_runs;
cout << "Number of hits = ";
cin >> players[i].hits;
}
}
void output(player players[2])
{
for (int i = 0; i < 2; i++)
{
cout << "Data for player " << i + 1 << endl;
cout << "Name = " << players[i].name << endl;
cout << "Number of home runs = " << players[i].home_runs << endl;
cout << "Number of hits = " << players[i].hits << endl;
}
}

void update(player players[2], string player_name)


{
int index = find(players, player_name);
if (index != -1)
{
cout << "Enter new data for player " << index + 1 << endl;

cout << "Name = ";


cin >> players[index].name;
cout << "Number of home runs = ";
cin >> players[index].home_runs;
cout << "Number of hits = ";
cin >> players[index].hits;
}
}
int find(player players[2], string player_name)
{
for (int i = 0; i < 3; i++)
{
if (players[i].name == player_name)
{
return i;
}
}
return -1;
}

};

int main()
{
player players[3];
int index;
char choice;
bool flag = true;
string player_name;

do
{
cout << endl;
cout << "Input data " << endl;
cout << "Update player " << endl;
cout << "Output data " << endl;
cout << "Exit" << endl;
cout << "Enter your choice = ";
cin >> choice;

switch (choice)
{
case 'i':
players[0].input(players);
break;
case 'u':
cin.ignore();
cout << "Enter player name = ";
cin >> player_name;
players[0].update(players, player_name);
break;
case 'e':
cout << "Program Terminated " << endl;
flag = false;
break;
case 'o':
players[0].output(players);
break;
default:
cout << "Invalid choice " << endl;
break;
}
} while (flag);
system("pause");
return 0;
}

Output:
Task-4:

#include<iostream>
#include<string>
using namespace std;
class person
{
private:
string name;
int age;
bool male;
string occupation;
bool cook;
public:
void inputs(string &n, int &age, bool &male, string &occupation, bool &cook)
{
char choice;
cout << "Enter name = ";
cin >> n;
cout << "Enter age = ";
cin >> age;
cout << "Enter occupation = ";
cin >> occupation;
cout << "Are you male (y/n) = ";
cin >> choice;
if (choice == 'y')
{
male = true;
}
else
{
male = false;
}
cout << "Can you cook (y/n) = ";
cin >> choice;
if (choice == 'y')
{
cook = true;
}
else
{
cook = false;
}
}
void intidata(string n, int age, bool male, string occupation, bool cook, person
p[2],int i)
{
p[i].name = n;
p[i].age = age;
p[i].male = male;
p[i].occupation = occupation;
p[i].cook = cook;
}
void output(person p[2],int i)
{
cout << "Name = " << p[i].name << endl;
cout << "Age = " << p[i].age << endl;
if (p[i].male == true)
{
cout << "You are male" << endl;
}
else
{
cout << "You are not male" << endl;
}
cout << "Occupation = " << p[i].occupation << endl;
if (p[i].cook == true)
{
cout << "You can cook" << endl;
}
else
{
cout << "You cannot cook" << endl;
}
}

};

int main()
{
person p[2];
string n;
int age;
bool male;
string occupation;
bool cook;
for (int i = 0; i < 2; i++)
{
p[0].inputs(n, age, male, occupation, cook);
p[0].intidata(n, age, male, occupation, cook,p,i);
}
for (int i = 0; i < 2; i++)
{
p[0].output(p,i);
}
system("pause");
return 0;
}

Output:
Task-5:

#include <iostream>
#include <string>

using namespace std;

class BankAccount
{
private:
int account_number;
double balance;
string owner_name;

public:
void Account(int num, double bal, string name)
{
account_number = num;
balance = bal;
owner_name = name;
}

void dis()
{
cout << " Account Holder :" << owner_name << endl;
cout << " Account Number :" << account_number << endl;
cout << " Current Balance :" << balance << endl;
}

void deposit(double amount)


{
balance += amount;
cout << "Deposited $" << amount << " into account number " <<
account_number << endl;
}

void withdraw(double amount)


{
if (balance >= amount)
{
balance -= amount;
cout << "Withdrawn $" << amount << " from account number " <<
account_number << endl;
}
else
{
cout << "Insufficient balance to withdraw $" << amount << " from
account number " << account_number << endl;
}
}

double check_balance()
{
return balance;
}
};

int main()
{
int acc1, acc2;
double bal1, bal2;
string name1, name2;
cout << "Enter Account number: ";
cin >> acc1;
cout << "Enter Balance : ";
cin >> bal1;
cout << "Enter Account Holder Name : ";
cin >> name1;
cout << " 2nd Account \n " << endl;
cout << "Enter Account number: ";
cin >> acc2;
cout << "Enter Balance : ";
cin >> bal2;
cout << "Enter Account Holder Name : ";
cin >> name2;

BankAccount user1, user2;

user1.Account(acc1, bal1, name1);


user2.Account(acc1, bal2, name2);

double deposit_amount, withdraw_amount;

cout << "Enter amount to deposit into account number " << acc1 << " : ";
cin >> deposit_amount;
user1.deposit(deposit_amount);

cout << "Enter amount to withdraw from account number " << acc1 << ": ";
cin >> withdraw_amount;
user1.withdraw(withdraw_amount);

cout << " 2nd Account " << endl;

cout << "Enter amount to deposit into account number " << acc2 << ": ";
cin >> deposit_amount;
user2.deposit(deposit_amount);

cout << "Enter amount to withdraw from account number " << acc2 << ": ";
cin >> withdraw_amount;
user2.withdraw(withdraw_amount);
user1.dis();
cout << " 2nd Account \n " << endl;
user2.dis();
system("pause");
return 0;
}

Output:

You might also like