Answer to Question #8598 in C++ for Muammar Mubin

Question #8598
write a program bus ticket. program must have departure time, arrival time, list of the passengers and transit check point.
1
Expert's answer
2012-04-24T07:14:20-0400
Program& bus_ticket. cpp stores and reads data using file bus.txt. Firstly, you have to create database using function& “add” from program. Then you can add data (function “add”) or watch data from file (function “show”).
Input data format:
Bus number: integer
Department time:& hour(integer) :& min(integer)
Arrival time: hour(integer) :& min(integer)
Check point: string (input 0 as a check point to stop entering check points)
Passenger name: string (input 0 as a passenger name to stop entering passenger’s name)

This program was created with Microsoft Visual Studio Pro 2010.
#include<iostream>
#include<process.h>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

struct time
{
int hour;
int min;
};
ostream &operator<<(ostream &os, time &t){
os<<t.hour<<':'<<t.min;
return os;
}
istream &operator>>(istream &is, time &t){
char c1;
int h,m;
is>>h>>c1>>m;
if(!is) return is;
if(c1!=':')
{is.clear(ios_base::failbit);return is;}
t.hour=h;
t.min=m;
return is;}


void add(){
int number;
time dep_time;
time arrival_time;
string name,check;
ofstream outf;
outf.open("bus.txt",ios::app);
if(!outf)
{cout<<"unable to open \n"; return;}
cout<<"input bus number: "; cin>>number; outf<<" "<<number<<" ";
cout<<"\n input department time: ";cin>>dep_time; outf<<dep_time<<" ";
cout<<"\n input arrival time: "; cin>>arrival_time; outf<<arrival_time<<" ";
cout<<"\n input check points: \n";
do
{cout<<"\n enter check point name:\t";
cin>>check;
outf<<check<<" ";}while(check!="0");
cout<<"\n input passengers name: \n";
do
{cout<<"\n enter name of passenger \t";
cin>>name;
if(name!="0"){outf<<name<<" ";}else{outf<<name;}}while(name!="0");
if(!outf){cout<<"\n write is impossible \n";return;}
}

void show(){
int number;
time dep_time;
time arrival_time;
string name,check;
ifstream of;
of.open("bus.txt");
if(!of){cout<<"\n unable to open "; return;}
while(true){
if(of.eof()){return;}

cout<<"\n bus number is: \t";of>>number;cout<<number;
& cout<<"\n department time is: \t";of>>dep_time;cout<<dep_time;
& cout<<"\n arrival time is: \t";of>>arrival_time;cout<<arrival_time;
cout<<"\n check points are:\t";
& do
{

of>>check;
if(check!="0"){
cout<<"\n"<<check;}
}while(check!="0");
cout<<"\n passengers names are: \t";
do
{
of>>name;
if(name!="0"){
& cout<<"\n"<<name;}
}while(name!="0");


}
};
int main(){

void add();
void show();
char ch;
while(true)
{
cout<<"\n please make your choice \n"<<'a'<<"\t add a bus\n"<<'s'<<"\t show buses \n"<<'x'<<"\t exit \n"<<"your choice is: ";
cin>>ch;
switch(ch){
case 'a': add(); break;
case 's': show();break;
case 'x': exit(0);
default: cout<<"unknown command \n";
}}
return 0;}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS