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() {...
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;...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT