Saturday 29 October 2016

Hospital management system C++

Hospital management system C++

code:


#include "stdafx.h"
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include <iostream>

using namespace std;
// define maximum number of patients in a queue
#define MAXPATIENTS 100

// define structure for patient data
struct patient
{
char FirstName[50];
char LastName[50];
char ID[20];
};


// define class for queue
class queue
{
public:
queue (void);
int AddPatientAtEnd (patient p);
int AddPatientAtBeginning (patient p);
patient GetNextPatient (void);
int RemoveDeadPatient (patient * p);
void OutputList (void);
char DepartmentName[50];
private:
int NumberOfPatients;
patient List[MAXPATIENTS];
};


// declare member functions for queue

queue::queue ()
{
// constructor
NumberOfPatients = 0;
}


int queue::AddPatientAtEnd (patient p)
{
// adds a normal patient to the end of the queue.
// returns 1 if successful, 0 if queue is full.
if (NumberOfPatients >= MAXPATIENTS)
{
// queue is full
return 0;
}
// put in new patient
else
List[NumberOfPatients] = p; NumberOfPatients++;
return 1;
}


int queue::AddPatientAtBeginning (patient p)
{
// adds a critically ill patient to the beginning of the queue.
// returns 1 if successful, 0 if queue is full.
int i;
if (NumberOfPatients >= MAXPATIENTS)
{
// queue is full
return 0;
}

// move all patients one position back in queue
for (i = NumberOfPatients-1; i >= 0; i--)
{
List[i+1] = List[i];
}
// put in new patient
List[0] = p; NumberOfPatients++;
return 1;
}


patient queue::GetNextPatient (void)
{
// gets the patient that is first in the queue.
// returns patient with no ID if queue is empty

int i; patient p;
if (NumberOfPatients == 0) {
// queue is empty
strcpy_s(p.ID,"");
return p;}
// get first patient
p = List[0];
// move all remaining patients one position forward in queue
NumberOfPatients--;
for (i=0; i<NumberOfPatients; i++)
{
List[i] = List[i+1];
}
// return patient
return p;
}


int queue::RemoveDeadPatient (patient * p)
{
// removes a patient from queue.
// returns 1 if successful, 0 if patient not found
int i, j, found = 0;
// search for patient
for (i=0; i<NumberOfPatients; i++)
{
if (_stricmp(List[i].ID, p->ID) == 0)
{
// patient found in queue
*p = List[i]; found = 1;
// move all following patients one position forward in queue
NumberOfPatients--;
for (j=i; j<NumberOfPatients; j++)
{
List[j] = List[j+1];
}
}
}
return found;
}


void queue::OutputList (void)
{
// lists entire queue on screen
int i;
if (NumberOfPatients == 0)
{
cout << "Queue is empty";
}
else
{

for (i=0; i<NumberOfPatients; i++)
{
cout << "" << List[i].FirstName;
cout << " " << List[i].LastName;
cout << " " << List[i].ID;
}
}
}


// declare functions used by main:

patient InputPatient (void)
{
// this function asks user for patient data.
patient p;
cout << "Please enter data for new patient First name: ";
cin.getline(p.FirstName, sizeof(p.FirstName));
cout << "Last name: ";
cin.getline(p.LastName, sizeof(p.LastName));
cout << "Social security number: ";
cin.getline(p.ID, sizeof(p.ID));
// check if data valid
if (p.FirstName[0]==0 || p.LastName[0]==0 || p.ID[0]==0)
{
// rejected
strcpy_s(p.ID,"");
cout << "Error: Data not valid. Operation cancelled."<<endl;
_getch();
}
return p;
}


void OutputPatient (patient * p)
{
// this function outputs patient data to the screen
if (p == NULL || p->ID[0]==0)
{
cout << "No patient";
return;
}
else
cout << "Patient data:"<<endl;
cout << "First name: " << p->FirstName<<endl;
cout << "Last name: " << p->LastName<<endl;
cout << "Social security number: " << p->ID<<endl;
}


int ReadNumber()
{
// this function reads an integer number from the keyboard.
// it is used because input with cin >> doesn't work properly!
char buffer[20];
cin.getline(buffer, sizeof(buffer));
return atoi(buffer);
}


void DepartmentMenu (queue * q)
{
// this function defines the user interface with menu for one department
int choice = 0, success;
patient p;
while (choice != 6)
{
// clear screen
//clrscr();
// print menu
cout << "Welcome to department: " << q->DepartmentName<<endl;
cout << "Please enter your choice:"<<endl;
cout << "1: Add normal patient\n"<<endl;
cout << "2: Add critically ill patient\n"<<endl;
cout << "3: Take out patient for operation\n"<<endl;
cout << "4: Remove dead patient from queue\n"<<endl;
cout << "5: List queue\n"<<endl;
cout << "6: Change department or exit\n"<<endl;
// get user choice
choice = ReadNumber();
// do indicated action
switch (choice)
{
case 1: // Add normal patient
p = InputPatient();
if (p.ID[0])
{
success = q->AddPatientAtEnd(p);
//clrscr();
if (success)
{
cout << "Patient added:";

}
else
{
// error
cout << "Error: The queue is full. Cannot add patient:";
}
OutputPatient(&p);
cout << "Press any key";
_getch();
}
break;

case 2: // Add critically ill patient
p = InputPatient();
if (p.ID[0])
{
success = q->AddPatientAtBeginning(p);
//clrscr();
if (success)
{
cout << "Patient added:";
}
else
{
// error
cout << "Error: The queue is full. Cannot add patient:";
}

OutputPatient(&p);
cout << "Press any key";
_getch();
}
break;

case 3: // Take out patient for operation
p = q->GetNextPatient();
//clrscr();
if (p.ID[0])
{
cout << "Patient to operate:";
OutputPatient(&p);}
else
{
cout << "There is no patient to operate."<<endl;
}
cout << "Press any key";
_getch();
break;

case 4: // Remove dead patient from queue
p = InputPatient();
if (p.ID[0])
{
success = q->RemoveDeadPatient(&p);
//clrscr();
if (success)
{
cout << "Patient removed:";
}
else
{
// error
cout << "Error: Cannot find patient:"<<endl;
}
OutputPatient(&p);
cout << "Press any key";
_getch();
}
break;

case 5: // List queue
//clrscr();
q->OutputList();
cout << "Press any key";
_getch(); break;
}
}
}


// main function defining queues and main menu
void main ()
{
int i, MenuChoice = 0;
// define three queues
queue departments[3];
// set department names
strcpy_s (departments[0].DepartmentName, "Heart clinic");
strcpy_s (departments[1].DepartmentName, "Lung clinic");
strcpy_s (departments[2].DepartmentName, "Plastic surgery");

while (MenuChoice != 4)
{
// clear screen
//clrscr();
// print menu
cout << "Welcome to Software City Hospital"<<endl;
cout << "Please enter your choice:"<<endl;
for (i = 0; i < 3; i++)
{
// write menu item for department i
cout << "" << (i+1) << ": " << departments[i].DepartmentName<<endl;
}
cout << "4: Exit"<<endl;
// get user choice
MenuChoice = ReadNumber();
// is it a department name?
if (MenuChoice >= 1 && MenuChoice <= 3)
{
// call submenu for department
// (using pointer arithmetics here:)
DepartmentMenu (departments + (MenuChoice-1));
}
}
}

Calculator example C++

Calculator example C++

code:


#include<iostream>
using namespace std;
class calculater
{
public: int n1, n2, ans;
public: void plus();
void sub();
void devide();
void multiply();
void wtite();
void read();
void square();
void prime();


};
void calculater::plus()
{
system("cls");
cout << "\t\t Addition" << endl;
cout << "\t\t ........" << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1 + n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::sub()
{
system("cls");
cout << "\t\t Subtraction" << endl;
cout << "\t\t ..........." << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1 - n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::devide()
{
system("cls");
cout << "\t\t Division" << endl;
cout << "\t\t ........" << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1 / n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::multiply()
{
system("cls");
cout << "\t\t Multiplication" << endl;
cout << "\t\t .............." << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1*n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::square()
{
system("cls");
cout << "Square of number" << endl;
cin >> n1;
cout << "Square of number is =" << n1*n1 << endl;
system("pause");
}
void calculater::prime()
{
system("cls");
int a = 2;
cout << "Prime number" << endl;
cin >> n1;
while (n1%a != 0)
a++;
if (n1 == a)
cout << "Prime number" << endl;
else
cout << "Not prime" << endl;
system("pause");
}
void main()
{
calculater c;
char option;
do{
system("cls");
cout << "\t\t Advance Calculator" << endl;
cout << "\t\t...................." << endl;
cout << "1.Basic Calculator.....B" << endl;
cout << "2.Advance Calculator...A" << endl;
cout << "3.Exit.................E" << endl;
cout << "  Select option:";
cin >> option;
if (option == 'B')
{
system("cls");
cout << "\t\tBasic Calculator" << endl;
cout << "\t\t................" << endl;
cout << "1.Add..............+" << endl;
cout << "2.Subtract.........-" << endl;
cout << "3.Multiply.........*" << endl;
cout << "4.Divide.........../" << endl;
cout << "5.Exit.............E" << endl;
cout << "6.Select option:";
cin >> option;
if (option == '+')
{
c.plus();
}
else if (option == '-')
{
c.sub();
}
else if (option == '*')
{
c.multiply();
}
else if (option = '/')
{
c.devide();
}
}
if (option == 'A')
{
system("cls");
cout << "\t\tAdvance Calculator" << endl;
cout << "\t\t.................." << endl;
cout << "1.Square of number....S" << endl;
cout << "2.Prime number........P" << endl;
cout << "3.Back................B" << endl;
cout << "  select option:";
cin >> option;
if (option == 'S')
{
c.square();
}
else if (option == 'P')
{
c.prime();
}
}
} while (option != 'E');
}

C++ Bank management system project

C++ Bank management system project

Code:
#include<iostream>
#include<fstream>
using namespace std;
class account
{
    private:
        char account_number[20];
        char firstName[10];
        char lastName[10];
        float total_Balance;
    public:
       void deposit();
void read_data();
        void show_data();
        void write_rec();
        void read_rec();
        void search_rec();
        void edit_rec();

        
};
void account::read_data()
{
    cout<<"\nPlease Enter Account Number: ";
    cin>>account_number;
    cout<<"Enter First Name: ";
    cin>>firstName;
    cout<<"Enter Last Name: ";
    cin>>lastName;
    cout<<"Enter Balance: ";
    cin>>total_Balance;
    cout<<endl;
}
void account::show_data()
{
    cout<<"Account Number: "<<account_number<<endl;
    cout<<"First Name: "<<firstName<<endl;
    cout<<"Last Name: "<<lastName<<endl;
    cout<<"Current Balance: Rs.  "<<total_Balance<<endl;
    cout<<"-------------------------------"<<endl;
}
void account::write_rec()
{
    ofstream outfile;
    outfile.open("record.dat", ios::binary|ios::app);
    read_data();
    outfile.write(reinterpret_cast<char *>(this), sizeof(*this));
    outfile.close();
}
void account::read_rec()
{
    ifstream infile;
    infile.open("record.dat", ios::binary);
    if(!infile)
    {
        cout<<"File Not Found!!"<<endl;
        return;
    }
    cout<<"\n****Data from file****"<<endl;
    while(!infile.eof())
    {
        if(infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0)
        {
            show_data();
        }
    }
    infile.close();
}
void account::search_rec()
{
    int n;
    ifstream infile;
    infile.open("record.dat", ios::binary);
    if(!infile)
    {
        cout<<"\nError in opening! File Not Found!!"<<endl;
        return;
    }
    infile.seekg(0,ios::end);
    int count = infile.tellg()/sizeof(*this);
    cout<<"\n There are "<<count<<" record in the file";
    cout<<"\n Enter Record Number to Search: ";
    cin>>n;
    infile.seekg((n-1)*sizeof(*this));
    infile.read(reinterpret_cast<char*>(this), sizeof(*this));
    show_data();
}
void account::edit_rec()
{
    int n;
    fstream iofile;
    iofile.open("record.dat", ios::in|ios::binary);
    if(!iofile)
    {
        cout<<"\nError in opening! File Not Found!!"<<endl;
        return;
    }
    iofile.seekg(0, ios::end);
    int count = iofile.tellg()/sizeof(*this);
    cout<<"\n There are "<<count<<" record in the file";
    cout<<"\n Enter Record Number to edit: ";
    cin>>n;
    iofile.seekg((n-1)*sizeof(*this));
    iofile.read(reinterpret_cast<char*>(this), sizeof(*this));
    cout<<"Record "<<n<<" has following data"<<endl;
    show_data();
    iofile.close();
    iofile.open("record.dat", ios::out|ios::in|ios::binary);
    iofile.seekp((n-1)*sizeof(*this));
    cout<<"\n Modify Data "<<endl;
   deposit();
    iofile.write(reinterpret_cast<char*>(this), sizeof(*this));
}
void account::deposit()
{ int x;

cout<<"Enter amount that you want to add";
cin>>x;
total_Balance+=x;
}
int main()
{
    account A;
    int choice;
    cout<<"Acount Record System"<<endl;
    while(true)
    {
        cout<<"Select one option below ";
        cout<<"\n\t1 Add record to file";
        cout<<"\n\t2 Show record from file";
        cout<<"\n\t3 Search Record from file";
        cout<<"\n\t4 Update Record";
       
        cout<<"\n\t5-->Quit";
        cout<<"\nEnter your choice: ";
        cin>>choice;
        switch(choice)
        {
            case 1:
                A.write_rec();
                break;
            case 2:
                A.read_rec();
                break;
            case 3:
                A.search_rec();
                break;
            case 4:
                A.edit_rec();
                break;
            
               
            case 5:
                exit(0);
                break;
            default:
                cout<<"\nEnter corret choice";
                exit(0);
        }
    }
    system("pause");
    return 0;
}