C Language: The goal is to create a drug dispensing program that has 4 drugs and
ID: 3722137 • Letter: C
Question
C Language:
The goal is to create a drug dispensing program that has 4 drugs and allows a user to check quantities, refill, and dispense. A user must first login with the following credentials before the menu becomes available
Username: Admin01
Password: L3tM3in
The 4 drugs and their initial quantities are
Tylenol 40
Advil 40
Ibuprofen 30
Codeine 10
If either username or password are incorrect, console will clear, and the program will stay at the log in screen.
If credentials are correct and user presses enter, console will clear, and a menu will be displayed.
If option 1 is selected, the console is cleared, and the current quantity of each drug is displayed. A user presses b to go back to menu.
If option 2 is selected, the console is cleared, and a prompt asks a user to select a drug and then enter a quantity to refill with. The program updates quantity and goes back to the main menu
Need to select option 1 from main menu to see if results are updated
From the menu, if option 3 is selected, the program will prompt the user to select a drug and how much to dispense.
When the user presses enter, the program goes back to the main menu. Option 1 must be selected to verify result of dispense.
Explanation / Answer
Please find the below program.
Didn't got chance to verify all the test cases. but working for main functionality
#include <stdio.h>
#include <unistd.h>
#include <conio.h>
const char OrigUser[10]= "Admin01" ;
const char OrigPass[10]= "L3tM3in" ;
static int Tylenol=40;
static int Advil =40;
static int Ibuprofen =30;
static int Codeine =10;
int drug_code,drug_refill_amount,drug_dispense_amount;
int main()
{
char Username[10];
char Password[10];
char receive=' ';
int choice;
int backtoMainMenu =0 ;
do
{
clrscr();
printf("Username: ");
scanf("%s",&Username);
printf(" Password: ");
scanf("%s",&Password);
}while ((compare_strings(OrigUser,Username) != 0) && (compare_strings(OrigPass,Password) != 0));
for(;backtoMainMenu==0;)
{
clrscr();
printf(" Drug Dispenser ");
printf(" ------------------------------");
printf(" 1. Quantity of each drug: 1");
printf(" 2. Refill a specific drug: 2 ");
printf(" 3. Amount of drug Dispense: 3 ");
printf(" 4. Exit: 4 ");
printf(" ------------------------------");
printf(" Enter Your Choice: ");
scanf("%d",&choice);
receive=' ';
switch(choice)
{
case 1:
printQuanties();
for(;receive!='b';)
{
scanf("%c",&receive);
printf(" Press b to go back to Main menu : ");
}
backtoMainMenu=0;
break;
case 2:
refillDrugs();
backtoMainMenu=0;
break;
case 3:
dispenseDrugs();
backtoMainMenu=0;
break;
case 4:
exit(0);
}
}
return 0;
}
void refillDrugs()
{
printf(" 1. Select Drug for Refill");
printf(" ------------------------------");
printf(" 1. Tylenol : 11");
printf(" 2. Advil : 22 ");
printf(" 3. Ibuprofen: 33 ");
printf(" 4. Codeine : 44 ");
printf(" Enter drug code ");
scanf("%d",&drug_code);
printf(" Enter amount to refill: ");
scanf("%d",&drug_refill_amount);
switch(drug_code)
{
case 11:
Tylenol=drug_refill_amount;
break;
case 22:
Advil=drug_refill_amount;
break;
case 33:
Ibuprofen=drug_refill_amount;
break;
case 44:
Codeine=drug_refill_amount;
break;
default:
printf(" INVALID SELECTION...Please try again");
}
}
void dispenseDrugs()
{
printf(" 1. Select Drug for dispense");
printf(" ------------------------------");
printf(" 1. Tylenol : 11");
printf(" 2. Advil : 22 ");
printf(" 3. Ibuprofen: 33 ");
printf(" 4. Codeine : 44 ");
printf(" Enter drug code ");
scanf("%d",&drug_code);
printf(" Enter amount to dispense: ");
scanf("%d",&drug_dispense_amount);
switch(drug_code)
{
case 11:
Tylenol=Tylenol - drug_dispense_amount ;
break;
case 22:
Advil=Advil-drug_dispense_amount;
break;
case 33:
Ibuprofen=Ibuprofen-drug_dispense_amount;
break;
case 44:
Codeine=Codeine-drug_dispense_amount;
break;
default:
printf(" INVALID SELECTION...Please try again");
}
}
void printQuanties()
{
printf(" ------------------------------");
printf(" Current Quantity of drugs: ");
printf(" ------------------------------");
printf(" Tylenol : %d",Tylenol);
printf(" Advil : %d",Advil);
printf(" Ibuprofen: %d",Ibuprofen);
printf(" Codeine : %d",Codeine);
printf(" ------------------------------");
}
int compare_strings(char a[], char b[])
{
int c = 0;
while (a[c] == b[c])
{
if (a[c] == '' || b[c] == '')
break;
c++;
}
if (a[c] == '' && b[c] == '')
return 0;
else
return -1;
}
#include <stdio.h>
#include <unistd.h>
#include <conio.h>
const char OrigUser[10]= "Admin01" ;
const char OrigPass[10]= "L3tM3in" ;
static int Tylenol=40;
static int Advil =40;
static int Ibuprofen =30;
static int Codeine =10;
int drug_code,drug_refill_amount,drug_dispense_amount;
int main()
{
char Username[10];
char Password[10];
char receive=' ';
int choice;
int backtoMainMenu =0 ;
do
{
clrscr();
printf("Username: ");
scanf("%s",&Username);
printf(" Password: ");
scanf("%s",&Password);
}while ((compare_strings(OrigUser,Username) != 0) && (compare_strings(OrigPass,Password) != 0));
for(;backtoMainMenu==0;)
{
clrscr();
printf(" Drug Dispenser ");
printf(" ------------------------------");
printf(" 1. Quantity of each drug: 1");
printf(" 2. Refill a specific drug: 2 ");
printf(" 3. Amount of drug Dispense: 3 ");
printf(" 4. Exit: 4 ");
printf(" ------------------------------");
printf(" Enter Your Choice: ");
scanf("%d",&choice);
receive=' ';
switch(choice)
{
case 1:
printQuanties();
for(;receive!='b';)
{
scanf("%c",&receive);
printf(" Press b to go back to Main menu : ");
}
backtoMainMenu=0;
break;
case 2:
refillDrugs();
backtoMainMenu=0;
break;
case 3:
dispenseDrugs();
backtoMainMenu=0;
break;
case 4:
exit(0);
}
}
return 0;
}
void refillDrugs()
{
printf(" 1. Select Drug for Refill");
printf(" ------------------------------");
printf(" 1. Tylenol : 11");
printf(" 2. Advil : 22 ");
printf(" 3. Ibuprofen: 33 ");
printf(" 4. Codeine : 44 ");
printf(" Enter drug code ");
scanf("%d",&drug_code);
printf(" Enter amount to refill: ");
scanf("%d",&drug_refill_amount);
switch(drug_code)
{
case 11:
Tylenol=drug_refill_amount;
break;
case 22:
Advil=drug_refill_amount;
break;
case 33:
Ibuprofen=drug_refill_amount;
break;
case 44:
Codeine=drug_refill_amount;
break;
default:
printf(" INVALID SELECTION...Please try again");
}
}
void dispenseDrugs()
{
printf(" 1. Select Drug for dispense");
printf(" ------------------------------");
printf(" 1. Tylenol : 11");
printf(" 2. Advil : 22 ");
printf(" 3. Ibuprofen: 33 ");
printf(" 4. Codeine : 44 ");
printf(" Enter drug code ");
scanf("%d",&drug_code);
printf(" Enter amount to dispense: ");
scanf("%d",&drug_dispense_amount);
switch(drug_code)
{
case 11:
Tylenol=Tylenol - drug_dispense_amount ;
break;
case 22:
Advil=Advil-drug_dispense_amount;
break;
case 33:
Ibuprofen=Ibuprofen-drug_dispense_amount;
break;
case 44:
Codeine=Codeine-drug_dispense_amount;
break;
default:
printf(" INVALID SELECTION...Please try again");
}
}
void printQuanties()
{
printf(" ------------------------------");
printf(" Current Quantity of drugs: ");
printf(" ------------------------------");
printf(" Tylenol : %d",Tylenol);
printf(" Advil : %d",Advil);
printf(" Ibuprofen: %d",Ibuprofen);
printf(" Codeine : %d",Codeine);
printf(" ------------------------------");
}
int compare_strings(char a[], char b[])
{
int c = 0;
while (a[c] == b[c])
{
if (a[c] == '' || b[c] == '')
break;
c++;
}
if (a[c] == '' && b[c] == '')
return 0;
else
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.