Combining two programs into one

Pages: 12
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.

First program:
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
  /* Date: 5/8/2019
	Purpose: Calculate insulin dosage.
	*/
	
#include <iostream>
using namespace std;

int main ()
{
	//Step 1: Declare the variables.
	int current_BG;
	const double ideal_BG = 125;
	const double 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;
	}
	
	else if (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;
		
	}
	
	else if (current_BG < ideal_BG)
	{
		cout << "You don't need any correction at all! Your BG is low!\n";
		cout << "Go drink a soda!" << endl;
	}
}


Second Program:
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
  #include <iostream>
using namespace std;

int main ()
{
	int meal_Carbs;
	const double 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;
	}
	
	else if (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;
	}
}
Why? Which one would you call the "main" program? Just make the other one a func, ya know? What is the purpose of combining them?
Last edited on
Also, why don’t you make them to different funcs and just have one call the other? And what are the errors?
Last edited on
I wanted to make it where instead of going back and forth from the two programs, I would make them into one and save time.
Oh ok alright then, let’s see...
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?
At first, I tried that. However, I may have done something wrong. I remember I did
1
2
3
4
if (current_BG == ideal_BG && meal_Carbs < carb_Factor)
{
cout << "You do not need to make any corrections." << endl;
}


Did this to have both the correction and carb count calculated, but it would only show the results for the correction part and not the carb count.
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
So I just change the int main () on both programs and that's it?
Sorry if I frustrate everyone here for not understanding what to do. I'm new to C++.
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?
Wait no got it sry.
Yeah that would work it looks like.
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.
Oops I thought salem was horror for a second sry guys XD and yeah yay exactly
Last edited on
So this is the insulin.cpp program:
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
#include <iostream>
using namespace std;

void main_insulin ()
{
	//Step 1: Declare the variables.
	int current_BG;
	const double ideal_BG = 125;
	const double 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;
	}
	
	else if (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;
		
	}
	
	else if (current_BG < ideal_BG)
	{
		cout << "You don't need any correction at all! Your BG is low!\n";
		cout << "Go drink a soda!" << endl;
	}
}
This is the carbs.cpp program:
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
#include <iostream>
using namespace std;

void main_carbs ()
{
	int meal_Carbs;
	const double 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;
	}
	
	else if (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;
	}
}
And this is the new main.cpp program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main () 
{
	cout << "Choose";
    int choice;
    cin >> choice;
    if (choice == 1) 
	{
    	main_insulin ();
    }
	else 
	{
    	main_carbs ();
    }
}
Um. You forgot to include the carbs and insulin funcs...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
#include "carbs.cpp"
#include "insulin.cpp"

int main(){
   cout << "choose\n";
   int choice;
   cin >> choice;
   if(choice == 1){
      main_insulin();
   }
   else {
      main_carbs();
   }
   return 0;
}


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.
Last edited on
It works! Thanks, everyone!
Pages: 12