Need assistance with assignment!

I'm fairly new to programming and I was given an assignment:

Define a pharmacy type class containing fields - unique name (up to 40 characters), purpose (up to 30 characters), side effects (yes or no), quantity available at the pharmacy store (integer ), unit cost of the medicine (two decimal places), date of manufacture.

Write a program using member functions that:

enter the pharmacy data set in a one-dimensional array of 1000 drugs;

finds the average value of all drugs that have side effects;

sort by drug name in alphabetical order;

identifies the drug with the highest value among drugs whose names start with the letters "A" and "C";

I have tried to do the code myself, but I haven't gotten any luck so far. I've tried referencing code from several forums online too, but there's been no luck so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string.h>
using namespace std;

class pharmacy 
{
    char purp[30];
    char sf[3];
    int quantity;
    double price;
public:
    char name[40];
    pharmacy();
    ~pharmacy();
    void display();
    friend void sort_price(int, pharmacy[]);
};

pharmacy::pharmacy()
{
    cin.sync();
    cout<<"Input drug name: ";
    cin.getline(name, 40);
    cout<<"Input purpose: ";
    cin.getline(purp, 30);
    cout<<"Are there side effects: ";
    cin.getline(sf, 3);
    cout<<"Input drug quantity: ";
    cin>>quantity;
    cout<<"Input drug price: ";
    cin>>price;
}

void pharmacy::display()
{
    cout<<"Drug name: "<<name<<endl;
    cout<<"Purpose: "<<purp<<endl;
    cout<<"Side effects: "<<sf<<endl;
    cout<<"Drug quanity: "<<quantity<<endl;
    cout<<"Drug price: "<<price<<endl;
}

pharmacy::~pharmacy()
{
    cout<<"~pharmacy"<<endl;
};

void sort_name(int br, pharmacy A[])
{
    pharmacy swap;
    for(int i=0; i<br-1; i++)
    for(int j=0; j<br-i-1; j++)
    if(strcmp(A[j].ime, A[j+1].ime)>0)
    {
        swap=A[j];
        A[j]=A[j+1];
        A[j+1]=swap;
    }
}



int main() 
{

    system ("pause");
    return 0;
}


Apologies if any of the used terms are not correct, it's been translated using Google Translate. If anyone can provide a working code or a similar code to the one that is being asked of me so that I can reference it, I'll be eternally grateful! Please help me, I have almost no time left!
Last edited on
enter the pharmacy data set in a one-dimensional array of 1000 drugs;

maybe this did not translate well. do you have a file of data, or is this just saying UP TO 1000?

because it says this:
Write a program using member functions that:

I think you need 2 classes. you need a 'drug' class, and a pharmacy class that has those.

so something like
struct drug
{
char unique_name[41]; //+1 for zero terminal above listed requirement
char purpose[31];
bool side_effects;
int quantity;
double cost;
int date; //what format? 20190504?
};

and
class pharmacy
{
drug alldrugs[1000];
int howmanydrugs;
//methods // average / sort / etc .. the stuff asked for
}

Like that?
quick example of the idea:
1
2
3
4
5
6
7
8
double pharmacy::average_of_has_sideffects()
{
    double average = 0;
    for(i= number of drugs you have, is this 1000?) 
      average+= alldrugs[i].cost * alldrugs[i].side_effects; //cheating a little bool is 1 or zero
   average /= numberofdrugs;
   return average;
}


Last edited on
@jonnin

The assignment said UP TO. Apparently, it didn't translate well.
As much as I'm conecrned, I think the whole pharamcy is the class, whereas the drugs and their stuff are fields from it.
Side effects should be represented as a bool for yes/no (true/false). You also didn't include manufacturing date, which should be put into a string or 3 separate integers (only if you plan on comparing dates since this would be more of a hassle).
@zapshe

I plan on just having the year. Even then, will an int cut it or do I need a char/string?
if he did the dates the way I did you can compare them safely as a single int. the yyyymmdd format as an integer is great :) You will need an integer capable of holding at least 20200000, possibly long long. (or you can subtract a fixed amount like 18000000 and use a smaller value).

check above I was still editing my post.
I think making it all in one class is going to mess you up. (Ok, I know it is. I highly recommend you do what I said if you want to get done with it rapidly).

if it is UP TO, you need to track how many you have currently with a private class variable (in pharmacy class in my example), if you do not yet know <vector>.

money is often handled in integers. if your currency is friendly to it like $USA you can choose to use integer instead of double and make 1 == a penny or whatever you are using. Regardless you need 2 decimal places when you print it .. however you want to do that.
Last edited on
@jonin

I still haven't learned about the vectors and the like. Also, I have to use double since I was instructed that I have to use the ios::flags(2) because that's how many signs I need after the decidecimal comma.
that is fine. use what you were told :)
you need a private variable to track how many you have is all. just an extra integer.
double is fine. ios:: and all are fine.

can you do it from here?
Last edited on
@jonin

Sadly no, not really. Honestly, I have no idea what am I even doing.
I was hoping for finding some references online on how to do the code, but I got nothing.
I doubt any college/university would allow vectors on assignments, they want students to be able to manage their own memory and such.

I don't know how much you've learned, but you should practice coding and such on your own time. It's a skill you need to be good at. I find my coding classes like most other classes, a place where students remember little things for as long as the class requires it, then the information is lost once you pass. So you should like coding (learning shouldn't be stressful but will probably be if you're learning in class and have assignments you need to turn in). I recommend learncpp.com .

Unfortunately, it's a bit of a rule to not solve homework and simply give finished code to people, since you wont learn from that - and writing someone else's code for an assignment isn't very pleasing.

Just tackle one issue at a time. You've gotten this far. Don't be afraid to just start typing code. You'll learn whether or not your code works. As long as your code compiles, you should get some credit. However, I'd assume non-compiling code will be an automatic 0 from most professors.
I can't say it much better than that...
start coding and see where you get, try to do one thing at a time.
make the class with the data asked for.
cobble up something that holds 1000 of them.
write something that can let you input the data.
try to do the average part... I gave you 90% of it …
see where you get stuck once you try it, and ask a question... repeat...
Topic archived. No new replies allowed.