Combining two programs into one with a menu

Hey there guys!

I am working on some homework for my Programming class and I had to create two programs: one that converted pounds and ounces to kilograms and grams and the other one kilograms and grams to pounds and ounces. I figured out how to do both programs, but the last part of the assignment is having to combine the two programs with a menu and allow the user to pick from one or the other then input all the numbers and so forth. I have never done something like this and could use some help on it. The first one changes pounds / ounces to kilos and the second vice versa. Thanks in advance!

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  
#include <iostream>
using namespace std;

void convertWeight(double ounces, double &grams, double pounds, double &kilograms);
double output(double ounces, double pounds);

int main()
{
	double ounces;
	double pounds;

	char answer;

	do
	{
		cout << "Enter weight in pounds and ounces: " << endl;
		cin >> pounds >> ounces;
		cout << output(pounds, ounces) << endl;

		cout << "Do you want to test again (y/n)?: " << endl;

		cin >> answer;

	} while (answer == 'y' || answer == 'Y');

	return 0;
}

double output(double ounces, double pounds)
{
	double grams;
	double kilograms;

	convertWeight(ounces, grams, pounds, kilograms);

	cout << pounds << " pounds and " << ounces << " ounces = "
		<< kilograms << " kilograms and " << grams << " grams: " << endl;

	return 0;
}

void convertWeight(double ounces, double &grams, double pounds, double &kilograms)
{
	kilograms = (pounds + ounces / 1000000) / 2.20462;
	grams = kilograms * 1000;
}

 
#include <iostream>
using namespace std;

void convertWeight(double grams, double &ounces, double kilograms, double &pounds);
double output(double grams, double kilograms);

int main()
{
	double grams;
	double kilograms;

	char answer;

	do
	{
		cout << "Enter weight in kilograms and grams: " << endl;
		cin >> kilograms >> grams;
		cout << output(kilograms, grams) << endl;

		cout << "Do you want to test again (y/n)?: " << endl;

		cin >> answer;

	} while (answer == 'y' || answer == 'Y');

	return 0;
}

double output(double grams, double kilograms)
{
	double ounces;
	double pounds;

	convertWeight(grams, ounces, kilograms, pounds);

	cout << kilograms << " kilograms and " << grams << " grams = "
		<< pounds << " pounds and " << ounces << " ounces: " << endl;

	return 0;
}

void convertWeight(double grams, double &ounces, double kilograms, double &pounds)
{
	pounds = ( grams / 1000 + kilograms) / 0.453592 ;
	ounces = pounds * 16;
}
A C++ program can only have one function named main(). You probably should rename both of these functions to something else and create a new main() that calls these functions.

Also while you can have multiple versions of other functions, the type and or number of parameters must be different. You have two functions named output that has two double parameters, this won't work. I suggest you change your function names using more meaningful variable names. Perhaps something like:

void output_grams(double grams, double kilograms);
void output_ounces(double ounces, double pounds);

Probably the same for the other function as well.

I see what you are saying with the main() being different and having two of them, but I still don't really understand how to combine the two so when I run the program I select which main I want to run and enter all of the information. Also when I try to change the output, the output inside the function becomes undefined, so I am not sure how to change that either.

Thanks.
Bump
jlb has already explained what you have to do.

Do you understand how to write a menu?
This will become your new main().

You''re also going to have to rename your existing main() functions so the names are unique and not main(). These new function names will be called from your new main (menu).
You will also have to make the output and convertWeight function names unique.




Last edited on
Topic archived. No new replies allowed.