In: Computer Science
Debug please. It's in C++
#include<iostream>
#include<string>
using namespace std;
class Prescription
{
friend ostream& operator<<(ostream&, const Prescription&);
friend istream& operator>>(istream&, Prescription&);
private:
int idNum;
int numPills;
double price;
public:
Prescription(const int = 0, const int = 0, const double = 0.0);
};
Prescription::Prescription(const int id, const int pills, const double p)
{
id = id;
numPills = pills;
price = p;
}
ostream& operator<<(ostream& out, const Prescription pre)
{
out << "Prescription #" << pre.idNum <<
" Pills: " << pre.numPills <<
" Price is $" << per.price << endl;
return out;
}
istream& operator>>(istream& in, Prescridtion& pre)
{
cout << endl;
cout << "Enter prescription number ";
in >> preidNum;
if(pre.idNum <= 0);
throw("Invalid ID");
cout << "Enter number of pills ";
in >> pre.numPills;
if(pre.numPills <= 0)
throws("Number of pills invalid");
cout << "Enter price ";
in >> pre.price;
if(pre.price < 0.0)
throw("Price is negative");
return in;
}
int main()
{
Prescription aScrip;
try
cin >> aScrip;
catch(const char *msg)
{
cout << msg << endl;
}
cout << aScrip;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Prescription {
friend ostream &operator<<(ostream &, const Prescription &);
friend istream &operator>>(istream &, Prescription &);
private:
int idNum;
int numPills;
double price;
public:
Prescription(const int = 0, const int = 0, const double = 0.0);
};
Prescription::Prescription(const int id, const int pills, const double p) {
this->idNum = id;
numPills = pills;
price = p;
}
ostream &operator<<(ostream &out, const Prescription &pre) {
out << "Prescription #" << pre.idNum << " Pills: " << pre.numPills << " Price is $" << pre.price << endl;
return out;
}
istream &operator>>(istream &in, Prescription &pre) {
cout << endl;
cout << "Enter prescription number ";
in >> pre.idNum;
if (pre.idNum <= 0)
throw "Invalid ID";
cout << "Enter number of pills ";
in >> pre.numPills;
if (pre.numPills <= 0)
throw "Number of pills invalid";
cout << "Enter price ";
in >> pre.price;
if (pre.price < 0.0)
throw "Price is negative";
return in;
}
int main() {
Prescription aScrip;
try {
cin >> aScrip;
}
catch (const char *msg) {
cout << msg << endl;
}
cout << aScrip;
return 0;
}