Assignment No. 03
Semester: Fall 2011
CS201:
Introduction to Programming
|
Total
Marks: 20
Due
Date:29/12/2011
|
||
Instructions:
Please
read the following instructions carefully before submitting assignment. It should be clear that your assignment will not get any
credit if:
§
The assignment is submitted after due date.
§
The submitted assignment does not open or file is corrupt.
§
Assignment is copied(partial or full) from any source (websites,
forums, students, etc)
Note:
You have to upload only .cpp file. Assignment in any other format (extension)
will not be accepted and will be awarded with zero marks. For example, if you
submit code in .doc (Word document) or .txt files, no reward will be given in
any case.
Objectives:
The
objective of this assignment is to provide hands on experience of:
§
Structures
§
Dynamic
Memory allocation in C/C++
§
String
Manipulation Functions
§
File
Handling
Guidelines:
§
Code
should be properly indented and well commented.
§
Follow
C/C++ rules while writing variable names, function names etc
§
Use
only dev-C++ for this assignment.
§
Use
appropriate C/C++ structure i.e. if-else; switch statement etc to get inputs
from user where required (Marks will be deducted if inappropriate structure
will be used).
|
|||
Problem Statement: Flight Reservation System
Write a program for
Flight Reservation System which reserves the tickets for passengers of an Air
Line according to their requirements.
Detailed Description:
In
the program, you are required to make a structure with name 'Reservation' and
following are the data members of structure.
ñ Passenger Name
ñ Passenger Address
ñ Date of flight
ñ Destination to travel
ñ Flight No
ñ Seat No
In
the main() function, create of
variable of type Reservation. The variable of type Reservation should be created
dynamically using memory allocation function. After that, create following
user defined function as discussed below:
GetData(),
this function will set the values of above created structure variable according
to the inputs taken from user. It will prompt user to enter required
information for the, Passenger
Name, Passenger Address, Destination, Date of flight. User will be provided
three options for the destination that is: Karachi, Lahore and Peshawar.
Assigning Flight No and Seat
No:
The
user will chose one of three destinations and program will assign an
appropriate Flight No for the passenger. The Flight No will be assigned as
below:
Destination Flight No.
Karachi 201
Peshawar 233
Lahore 241
After
Assigning Flight No, the Seat No. will be assigned randomly which can be any
number from 0 to 200.
After
assigning the values for the above created variable of Reservation structure,
the structure will be passed to another user defined function WriteData()
for writing it to the
file.
In
the function WriteData(), you are
required to create a new text file name “data.txt” in same folder/directory
where you have saved your .cpp file. Open this file by using file handling
functions and then write all Reservation information in that file.
At the end, read all the data from
file by creating a function named ReadData()
and display the file contents on the screen.
Points To Remember:
Following
points should be kept in mind and handled accordingly, otherwise marks will
be deducted.
ñ Reading and writing from text file, must be
done with standard file handling functions provided in handouts.
ñ All data members must be declared and
initialized with appropriate data type.
ñ Exceptional cases must be kept in mind and
handled accordingly while taking input from user.
ñ User
must be prompted if there is any error while:
◦
Creating
a file.
◦
Opening
a file for reading/ writing.
|
|||
==================================================================
Solution
// Yasir Javaed //
// Mc090200982 //
#include<time.h>
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
using namespace std; // Name space Declearation //
struct Reservation
{
char pname[50];
char paddress[100];
char dateflight[8];
char destination[20];
int seatnum,flinum;
};
char Get(Reservation*, Reservation*,char[] ,Reservation*,Reservation*,int*);
char Write(Reservation, Reservation,char[], Reservation,Reservation,int);
char Read(Reservation ,Reservation,char [],Reservation ,Reservation ,int );
main()
{
Reservation pesanger;
Get(&pesanger,&pesanger,pesanger.destination,&pesanger,&pesanger,&pesanger.flinum);
Write(pesanger,pesanger,pesanger.destination,pesanger,pesanger,pesanger.flinum);
Read(pesanger,pesanger,pesanger.destination,pesanger,pesanger,pesanger.flinum);
getch();
}
char Get(Reservation *name,Reservation *address,char dest[],Reservation *date,Reservation *seat,int* fnum)
{
int choose; // First function declear and arrays create //
char kar[10]="Karachi";
char psh[10]="Peshawar";
char lhr[10]="Lahore";
cout<<"Please enter the Pessanger name : ";
gets((*name).pname);//cin.get((*n1).pn,50);
cout<<"Please enter the pessanger address : ";
cin.get((*address).paddress,100);//cin.get(info.pa,100);
cout<<endl<<"No.\t\tDestination\t\tFlight No"<<endl;
cout<<"1\t\tKarachi\t\t\t 201\n";
cout<<"2\t\tPeshawar\t\t 233\n";
cout<<"3\t\tLahore\t\t\t 241\n";
cout<<"Choose the Destination :";
cin>>choose;
switch(choose)
{
case 1:
*fnum=201; // assigning flight number 201 to all Karachi Passengers //
strcpy(dest,kar); // copy the destination into file //
break;
case 2:
*fnum=233; // assigning flight number 233 to all Peshawar Passengers //
strcpy(dest,psh); // copy the destination into file //
break;
case 3:
*fnum=241; // assigning flight number 241 to all Lahore Passengers //
strcpy(dest,lhr); // copy the destination into file //
break;
default :
cout<<"Invalud entry";
}
srand(time(NULL)); // random generator time //
(*seat).seatnum=rand()%200;
cout<<"\nPlease enter the Date of flight : ";
cin>>(*date).dateflight;
cout<<endl<<"Flight.# is : "<<*fnum<<endl;
cout<<"Seat# is : "<<(*seat).seatnum<<endl;
}
char Write(Reservation name,Reservation address,char dest[],Reservation date,Reservation seat,int fnum)
{
ofstream of;
char fname[]="data.txt";
of.open(fname);
if (!of)
{
cout<<"File cant be open"<<endl; // display summary of all input data //
system("pause");
}
of<<"Pessanger Name is : "<<name.pname<<endl;
of<<"Pessanger address is : "<<address.paddress<<endl;
of<<"Pessanger destiny is : "<<dest<<endl;
of<<"Pessanger Flight # is : "<<fnum<<endl;
of<<"Pessanger Seat # is : "<<seat.seatnum<<endl;
of<<"Pessanger date of flight is : "<<date.dateflight<<"-2012"<<endl;
of.close();
}
char Read(Reservation name,Reservation address,char dest[],Reservation date,Reservation seat,int fnum)
{
ifstream of;
char fname[]="data.txt";
of.open(fname);
if (!of)
{
cout<<"File cant be open"<<endl; // format data save in the file like this //
system("pause");
}
cout<<"\n\n\nPessanger Name is : "<<name.pname<<endl;
cout<<"Pessanger address is : "<<address.paddress<<endl;
cout<<"Pessanger destiny is : "<<dest<<endl;
cout<<"Pessanger Flight # is : "<<fnum<<endl;
cout<<"Pessanger Seat # is : "<<seat.seatnum<<endl;
cout<<"Pessanger date of flight is : "<<date.dateflight<<endl;
}
No comments:
Post a Comment