Question

In: Computer Science

Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...

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;

}

Solutions

Expert Solution

#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;
}

Related Solutions

Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x)...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator<<(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator/(int factor) { double half = amount / factor; return hafl; } ostream& operator<<(ostream& o, TuitionBill) { o << t.student << " Tuition:...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class...
what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class DivSales { private:    int quarterSales[4];    static double totalSales; public:    void add(int, int, int, int);    int sales(int);    static double getValue()    {        return totalSales;    };    void DivSales::add(int s1, int s2, int s3, int s4)    {        quarterSales[0] = s1;        quarterSales[1] = s2;        quarterSales[2] = s3;        quarterSales[3] = s4;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT