I'm trying combine these two programs into one, but I'm having trouble doing so. I always keep getting errors whenever I combine them as a void function. If anyone could step me through this I would appreciate it.
/* Date: 5/8/2019
Purpose: Calculate insulin dosage.
*/
#include <iostream>
usingnamespace std;
int main ()
{
//Step 1: Declare the variables.
int current_BG;
constdouble ideal_BG = 125;
constdouble glucose_Correction = 25;
double mathResults;
//Step 2: Prompt the user to input their current BG.
cout << "What was your current BG (Blood Glucose)?" << endl;
//Wait for the input and store it in current_BG.
cin >> current_BG;
//Step 3: Calculate the Correction Bolus.
mathResults = ((current_BG - ideal_BG) / glucose_Correction);
//Calculate rounded number of the final results as (mathResults + 0.5)
int roundedResults = mathResults + 0.5;
//Step 4
cout << "Your blood glucose is: " << current_BG << " mg/dL.\n";
//Step 5: Declare a IF/ELSE statement for if the current_BG is either
//equal to, greater, or less than the ideal_BG.
if (current_BG == ideal_BG)
{
cout << "You do not need to make any corrections." << endl;
}
elseif (current_BG > ideal_BG)
{
cout << "You need to make corrections.\n";
cout << "You need a dosage of " << mathResults << ", or " << roundedResults << " unit(s) of insulin." << endl;
}
elseif (current_BG < ideal_BG)
{
cout << "You don't need any correction at all! Your BG is low!\n";
cout << "Go drink a soda!" << endl;
}
}
#include <iostream>
usingnamespace std;
int main ()
{
int meal_Carbs;
constdouble carb_Factor = 15;
double calculation;
cout << "What was your total meal carbohydrates?" << endl;
cin >> meal_Carbs;
calculation = (meal_Carbs / carb_Factor);
int roundedCalc = calculation + 0.5;
cout << "Your current carb count is: " << meal_Carbs << " g\n";
if (meal_Carbs < carb_Factor)
{
cout << "You don't need to make any corrections." << endl;
}
elseif (meal_Carbs > carb_Factor)
{
cout << "You need to make corrections.\n";
cout << "You need a dosage of " << calculation << ", or " << roundedCalc << " unit(s) of insulin." << endl;
}
}
I’m guessing you want to combine the total insulin dosage from both programs, yes? So why don’t we just put them one after another, but put both the if else statements at the end of the new combined function. What’s the problem there?
For the first program, rename int main to void main_insulin
For the second program, rename int main to void main_carbs
The new name doesn't really matter, so long as it's meaningful (and unique).
If necessary, rename the source files to be say insulin.cpp and carbs.cpp (for example).
Now create a new main.cpp, like so.
1 2 3 4
int main ( ) {
main_insulin();
main_carbs();
}
Or maybe you want a choice.
1 2 3 4 5 6 7 8 9 10
int main ( ) {
cout << "Choose";
int choice;
cin >> choice;
if ( choice == 1 ) {
main_insulin();
} else {
main_carbs();
}
}
Finally, compile everything together with g++ main.cpp insulin.cpp carbs.cpp
Nah don’t worry about it, yer fine. Also I’m not entirely sure what you mean by "change the int main()",do you mean change them to regular function names or actually change the contents of each of the main functions?
From how I'm understanding this, salem c wanted me to change the int main in both programs to void functions. Then by doing that, I create a new program called main.cpp that will access both insulin.cpp and carbs.cpp.
#include <iostream>
usingnamespace std;
void main_insulin ()
{
//Step 1: Declare the variables.
int current_BG;
constdouble ideal_BG = 125;
constdouble glucose_Correction = 25;
double mathResults;
//Step 2: Prompt the user to input their current BG.
cout << "What was your current BG (Blood Glucose)?" << endl;
//Wait for the input and store it in current_BG.
cin >> current_BG;
//Step 3: Calculate the Correction Bolus.
mathResults = ((current_BG - ideal_BG) / glucose_Correction);
//Calculate rounded number of the final results as (mathResults + 0.5)
int roundedResults = mathResults + 0.5;
//Step 4
cout << "Your blood glucose is: " << current_BG << " mg/dL.\n";
//Step 5: Declare a IF/ELSE statement for if the current_BG is either
//equal to, greater, or less than the ideal_BG.
if (current_BG == ideal_BG)
{
cout << "You do not need to make any corrections." << endl;
}
elseif (current_BG > ideal_BG)
{
cout << "You need to make corrections.\n";
cout << "You need a dosage of " << mathResults << ", or " << roundedResults << " unit(s) of insulin." << endl;
}
elseif (current_BG < ideal_BG)
{
cout << "You don't need any correction at all! Your BG is low!\n";
cout << "Go drink a soda!" << endl;
}
}
EDIT: though I do suggest making two .hpp files with prototypes of each func in each day one and include those instead; I personally always get problems when I include cpp files for reasons I don’t really understand. So
carbs.hpp:
void main_carbs();
insulin.hpp
void main_insulin();
And now change the earlier#include "carbs.cpp" to #include "carbs.hpp" obviously do it for both of course just me being lazy.