Question: Q: (C++only) Write a program that will track how many medications are on hand at a small clinic. The program uses a structure to keep
Q: (C++only) Write a program that will track how many medications are on hand at a small clinic. The program uses a structure to keep the medication name and quantity on hand. Your program should offer the user a menu with these choices: (1) Print the medication status; (2) Add pills to the on-hand count; (3) Remove or withdraw pills from the on-hand count; and (4) quit.
There are five functions in this program that are required: (1) main menu (2) populate pills (auto-populates the pill structure with names and pill counts with random numbers) (3) print status (prints the current on-hand counts) (4) add pills to the count (5) remove pills from the count
Perform input validation such that when the user selects to remove pills, they're first told how many pills are available. The validation should prevent them from entering a negative number or entering a number that's greater than the stock on hand.
Design your program in a loop, offering the main menu each time through the loop until the user elects to quit.
Your output should very closely resemble the output below:
Press 1 to view medication count status 2 to add to the pill count 3 to remove pills 4 to quit Enter your choice: 1 MEDICATION STATUS: On hand Medication On hand Medication On hand Medication ------- ---------- ------- ---------- ------- ---------- 21 Aspirin 12 Tylenol 24 Lipitor 4 Prilosec 10 Glucophage 1 Zocor 17 Toprol 4 Zithromax 8 Zoloft 11 Xanax 27 Wellbutrin 11 Flexeril 18 Prozac 18 Effexor 26 Adderall Press 1 to view medication count status 2 to add to the pill count 3 to remove pills 4 to quit Enter your choice: 2 Enter the medication number to add: 1 Aspirin 2 Tylenol 3 Lipitor 4 Prilosec 5 Glucophage 6 Zocor 7 Toprol 8 Zithromax 9 Zoloft 10 Xanax 11 Wellbutrin 12 Flexeril 13 Prozac 14 Effexor 15 Adderall Your choice: 1 How many pills to add to the count of the medication Aspirin: 14 New count of Aspirin: 35 Press 1 to view medication count status 2 to add to the pill count 3 to remove pills 4 to quit Enter your choice: 3 Enter the medication number to withdraw from inventory: 1 Aspirin 2 Tylenol 3 Lipitor 4 Prilosec 5 Glucophage 6 Zocor 7 Toprol 8 Zithromax 9 Zoloft 10 Xanax 11 Wellbutrin 12 Flexeril 13 Prozac 14 Effexor 15 Adderall Your choice: 9 How many pills of the medication Zoloft to withdraw; Available on hand is: 8 Choice --> 16 ERROR: you can only remove up to 8 How many do you want to withdraw? 8 New count of Zoloft: 0 Press 1 to view medication count status 2 to add to the pill count 3 to remove pills 4 to quit Enter your choice: 1 MEDICATION STATUS: On hand Medication On hand Medication On hand Medication ------- ---------- ------- ---------- ------- ---------- 35 Aspirin 12 Tylenol 24 Lipitor 4 Prilosec 10 Glucophage 1 Zocor 17 Toprol 4 Zithromax 0 Zoloft 11 Xanax 27 Wellbutrin 11 Flexeril 18 Prozac 18 Effexor 26 Adderall Press 1 to view medication count status 2 to add to the pill count 3 to remove pills 4 to quit Enter your choice: 4 Program ending; have a superior day.
here is example code here.
#include#include #include #include using namespace std; const int MAXRAND = 30; const int SIZE = 15; struct Pills { string description; int count; }; // you shouldn't need to alter these function prototypes void populatePills(Pills (&)[SIZE], string []); void printStatus(Pills[]); void addPills(Pills (&)[SIZE]); void removePills(Pills (&)[SIZE]); int mainMenu(); int main() { srand(time(0)); Pills pills[SIZE]; char keepGoing = 'Y'; string names[SIZE] = {"Aspirin", "Tylenol", "Lipitor", "Prilosec", "Glucophage", "Zocor", "Toprol", "Zithromax", "Zoloft", "Xanax", "Wellbutrin", "Flexeril", "Prozac", "Effexor", "Adderall" }; populatePills(pills, names); // Your code here cout << " Program ending; have a superior day." << endl; return 0; } int mainMenu() { // your code here to present the main menu with choices 1-4. // your function returns an integer to main() with the user's choice } // Do not alter the function header. // It passes the struct by reference to the function. void addPills (Pills (&tempPills)[SIZE]) { // your code here }
// Do not alter the function header. // It passes the struct by reference to the function. void removePills (Pills (&tempPills)[SIZE]) { // your code here } // do not alter this function; it's complete and ready for your use. // it receives the struct pills as its parameter. void printStatus (Pills tempPills[]) { cout << " MEDICATION STATUS: "; cout << "On hand Medication On hand Medication On hand Medication" << endl; cout << "------- ---------- ------- ---------- ------- ----------" << endl; for (int i = 0; i < SIZE; i++) { cout << setw(7) << right << tempPills[i].count << " " << setw(12) << left << tempPills[i].description << " "; if ((i + 1) % 3 == 0) cout << endl; } } // do not alter this function. // it auto-populates the struct with names and random quantities of medications. void populatePills (Pills (&tempPills)[SIZE], string tempNames[]) { for (int i = 0; i < SIZE; i++) { tempPills[i].description = tempNames[i]; tempPills[i].count = ((rand() % MAXRAND) + 1); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
