This program can be used with a high accuracy scale to count pills, pellets, capsules etc. By averaging the weight per item for n number of three items against the total weight of items, the program calculates the count of items to an accuracy of +-3 per 100. This is especially useful for larger quantities of small items.
Front-end and downloadable .exe still pending.
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
bool ContProg();
/*Precondition: User is prompted for choice to continue program.
Postcondition: true/false return depending on user input.*/
int WeightToCount();
/*Precondition: Weight of all entered pills is put in.
Postcondition: Returns count of pills.*/
string MedInput();
/*Precondition: Empty string
Postcondition: Returns count of pills.*/
bool ContProg() {
string yesNoResponse;
//Prompt user input.
cin >> yesNoResponse;
//Run while loop until correct input is given.
while ((yesNoResponse != "yes") && (yesNoResponse != "no")) {
cout << "Incorrect response, please enter \'yes\' or \'no\': ";
cin >> yesNoResponse;
}
/* Remove potential remaining newline characters potentially causing
issues with the input if the main function runs again.*/
cin.ignore();
//Return condition based on input.
if (yesNoResponse == "yes") {
return true;
}
else {
return false;
}
}
int WeightToCount() {
int counter = 0;
double input, threePillWeight = 0, pillCount, avgWeight,
totalWeight;
do {
cout << "Enter weight of three pills: ";
while (!(cin >> input)) {
cout << "Invalid input. Please enter a decimal or integer value: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
threePillWeight += input;
counter++;
if (counter < 2) {
cout << "You have entered " << counter
<< " weight. Type \'yes\' to enter another weight?\n";
}
else {
cout << "You have entered " << counter
<< " weights. Type \'yes\' to enter another weight?\n";
}
} while (ContProg());
cout << "Enter total weight of pills in bottle: ";
cin >> totalWeight;
avgWeight = threePillWeight / counter;
pillCount = (totalWeight / avgWeight) * 3;
return pillCount;
}
string MedInput() {
string input;
cout << "Enter medication name: ";
getline(cin, input);
return input;
}
int main() {
//Initialize empty vectors
vector<string> medName;
vector<int> medCount;
int count = 0;
char end = 'n';
do
{
medName.push_back(MedInput());
medCount.push_back(WeightToCount());
count++;
if (count < 2) {
cout << "You have entered " << count
<< " medication. Type \'yes\' to enter another medication?\n";
}
else {
cout << "You have entered " << count
<< " medications. Type \'yes\' to enter another medication?\n";
} //Correct input is evaluated by function call in while() call
} while (ContProg());
for (int i = 0; i < medCount.size(); i++) {
cout << medName[i] << " has " << medCount[i] <<
" pills left.\n";
}
do {
cout << "Type 'yes' to terminate the program: \n";
cin >> end;
} while (end != 'y');
cout << "Thank you for using this program!\n\n";
return 0;
}