I have tried a lot but unable to correct just 1 logical error in the
following code. This error is in the for loop, when fine and minutes
are printed as the garbage values. Please check it out:
#include<iostream>
#include<string>
using namespace std;
int m=0, min, k[10];
class parkedcar
{
string make;
int model;
string color;
string licnumber;
int minutesparked;
public:
parkedcar() //default constructor//
{
make=" ";
model=0;
licnumber=" ";
color=" ";
minutesparked=0;
}
void setmake(string mk)
{
make=mk;
}
void setmodel(int ml)
{
model=ml;
}
void setcolor(string c)
{
color=c;
}
void setlicnumber(string l)
{
licnumber=l;
}
void setminutesparked(int mnp)
{
minutesparked=mnp;
}
string getmake()
{
return (make);
}
int getmodel()
{
return (model);
}
string getcolor()
{
return (color);
}
string getlicnumber()
{
return (licnumber);
}
int getminutesparked()
{
return (minutesparked);
}
void print();
};
void parkedcar::print() //outside class definition//
{
cout<<"Car Make :"<<getmake()<<endl;
cout<<"Car Model :"<<getmodel()<<endl;
cout<<"Car License Number :"<<getlicnumber()<<endl;
cout<<"Car Color :"<<getcolor()<<endl;
}
class parkingticket: public parkedcar
{
int fine;
int minutes;
void calfine();
public:
void showfine();
void getticket (int min)
{
minutes=min;
calfine();
cout<<endl;
}
void setmin(int mnl)
{
minutes=mnl;
}
int getfine()
{
return(fine);
}
};
void parkingticket::calfine()
{
if(minutes/60<=0)
{
fine = 125;
}
else
{
int mn;
mn=minutes - 60;
fine = 125 + 100 + 100*(mn/60);
}
}
void parkingticket::showfine()
{
cout<<"ILLEGAL PARKING"<<endl;
cout<<"Time in violation is "<<minutes/60<<" hours and "<<minutes
%60<<" minutes"<<endl;
cout<<"Fine : Rs. "<<fine<<endl;
}
class parkingmeter
{
int minpurchased;
public:
parkingmeter()
{
minpurchased=0;
}
void setminpurchased(int m)
{
minpurchased=m;
}
int getminpurchased()
{
return(minpurchased);
}
void print()
{
cout<<"Mins purchased are"<<getminpurchased();
}
};
class policeofficer
{
string name;
string badgenumber;
parkingticket *ticket;
public:
policeofficer()
{
name=" ";
badgenumber=" ";
ticket = NULL;
}
void setname(string n)
{
name=n;
}
void setbadgenumber(string b)
{
badgenumber=b;
}
string getname()
{
return(name);
}
string getbadgenumber()
{
return(badgenumber);
}
parkingticket* patrol(parkingticket pc1, parkingmeter pc2)
{
if ( pc1.getminutesparked() < pc2.getminpurchased() ||
pc1.getminutesparked()==pc2.getminpurchased() )
{
return NULL;
}
else
{
parkingticket t1[10];
t1[m].getticket(pc1.getminutesparked() - pc2.getminpurchased());
t1[m].showfine();
ticket=&t1[m];
return(ticket);
}
}
void print()
{
cout<<"Name: "<<name<<endl;
cout<<"Badge Number: "<<badgenumber<<endl;
}
};
void intro()
{
cout<<"******************************************* *************************************"<<endl;
cout<<" THIS IS PARKING TICKET SIMULATOR "<<endl;
cout<<"******************************************* *************************************"<<endl;
}
int main()
{
system("cls");
int c;
int i, j=0;
intro();
cout<<"Parking rate is Rs. 75 for 60 minutes"<<endl;
cout<<"In case number of minutes car has been parked exceeds the
number of minutes purchased, then a fine is applied."<<endl;
cout<<"Fine rates :"<<endl;
cout<<"Rs 125 for first hour or part of an hour that the car is
illegally parked"<<endl;
cout<<"And Rs 100 for every additional hour or part of an hour that
the car is illegally parked."<<endl;
cout<<"--------------------------------------------------"<<endl;
//Police Officer object created
policeofficer officer;
string pname, pbadge;
cout<<"OFFICER'S INFORMATION"<<endl;
cout<<"Name: ";
cin>>pname;
cout<<"Badge Number: ";
cin>>pbadge;
officer.setname(pname);
officer.setbadgenumber(pbadge);
cout<<"--------------------------------------------------"<<endl;
//Parking Ticket object created
parkingticket tck[10];
//Parking meter object created
parkingmeter meter[10];
do
{
parkingticket* ticket = NULL;
system("cls");
while(m<=j)
{
intro();
cout<<"Plese fill in the follwing details:"<<endl;
cout<<"--------------------------------------------------"<<endl;
cout<<"VEHICLE'S INFORMATION"<<endl;
string mk;
cout<<"Make :";
cin>>mk;
tck[m].setmake(mk);
int mod;
cout<<"Model :";
cin>>mod;
tck[m].setmodel(mod);
string lic;
cout<<"License number :";
cin>>lic;
tck[m].setlicnumber(lic);
string col;
cout<<"Color :";
cin>>col;
tck[m].setcolor(col);
int parmin;
cout<<"Minutes in parking :";
cin>>parmin;
tck[m].setminutesparked(parmin);
int purmin;
cout<<"Minutes purchased :";
cin>>purmin;
meter[m].setminpurchased(purmin);
system("cls");
intro();
//The officer patrols
ticket = officer.patrol(tck[m], meter[m]);
if(ticket==NULL)
{
// Display the ticket information.
tck[m].print();
cout<<"No crimes committed"<<endl;
}
else
{
cout<<endl<<"Non-permitted vehicle ::"<<endl;
// Display the ticket information.
tck[m].print();
ticket = NULL;
}
k[m] = tck[m].getminutesparked() - meter[m].getminpurchased();
m++;
} //while loop ends
cout<<"========================================="< <endl;
cout<<"Enter your choice -"<<endl;
cout<<"0 - Patrol another car"<<endl<<"1 - View cars
patrolled"<<endl<<"2 - Exit"<<endl;
cin>>c;
if(c==0)
{
j++;
}
else if(c==1)
{
break;
}
else
{
exit(0);
}
}while (c == 0);
system("cls");
intro();
for(i=0;i<m;i++)
{
tck[i].print();
if (k[i]>0)
{
tck[i].showfine();
}
else
{
cout<<"Vehicle permitted for "<<-k[i]<<" minutes"<<endl;
}
cout<<"__________________________________"<<endl;
}
cout<<"Officer responsible for issuing these tickets and
patroling ::"<<endl;
officer.print();
return 0;
}