Re: Help me out to correct logical error in this code
In article <fd1feef3-a6ac-439f-ad6b-ebb9f5627c9b@
79g2000hsk.googlegroups.com>, anchitgood@gmail.com says...
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:
Just glancing through the code, I think you have more than one error in
logic.
#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
This, for example, asserts that a parkingticket can be substituted for a
parkedcar under any possible circumstance. That sounds pretty far-
fetched at least to me.
[ ... ]
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);
The logic of this for loop does not seem to match the description
(instructions) given. In C++, you should also generally forget that the
'exit' function exists -- there is virtually never a good reason to use
it.
Right now, I think your code is badly polluted with imitation
encapsulation -- most of your classes are really structs in disguise,
with the 'set' and 'get' functions accomplishing nothing useful at all.
Until/unless you do more than just pass values through the get/set
member functions, you might as well just make the data public and be
done with it. If I were doing this, I'd think hard about overloading
operator>> and operator<< for some of the types involved so you can
encapsulate those parts a bit.
Just for example, I'd have the officer class something vaguely like
this:
struct officer {
std::string name_;
std::string number_;
officer(std::string const &name, std::string const &number) :
name_(name), number_(number)
{}
friend std::ostream &
operator<<(std::ostream &os, officer const &o) {
return os << "Name: " << o.name_ << "\n"
<< "Badge Number: " << o.number_ << "\n";
}
};
and possibly one more member something like:
static officer *get_data(std::istream &is, std::ostream &os) {
std::string name, number;
os << "\nPlease Enter Officer's data:\n";
os << "Name: ";
std::getline(is, name);
os << "Badge Number: ";
std::getline(is, number);
return new officer(name, number);
}
As it stands right now, the rest of your code knows about the internals
of an officer object -- that an officer has a name and a badge number,
and even that both of those are strings. What you want is for the rest
of the code to treat an officer as nothing more or less than an officer.
The officer object itself knows what's needed for an officer, how to
display data about itself, and so on.
The ticket and car data should be roughly similar -- the outside code
should know nothing about the ticket except relatively high-level
operations like 'write a ticket to a stream' (which should automatically
include write the data for the car and the ticketing officer).
--
Later,
Jerry.
The universe is a figment of its own imagination.