how can I apply the "USER-DEFINED FUNCTION" in this source code?help me pleasee

//UNIVERT
#include <iostream>
using namespace std;
int main()
{
//VARIABLES
int cm, inch, mile, kilometer, meter, foot, pounds, kg, yards, metre, grams, mg, Oz, Tons, mm,fahrenheit, celsius, kelvin, conversion;
cout<<"*******************Welcome to UNIVERT*******************"<<endl;
cout<<" "<<endl;
cout<<"***************UNIVERSAL UNIT CONVERTER*******************"<< endl;
cout<<" "<<endl;

cout<<"**THE HARDER YOU WORK FOR SOMETHING, THE GREATER YOU'LL FEEL WHEN YOU ACHIEVE IT**"<< endl;
cout<<" "<<endl;


//OPERATIONS
cout<< "LIST OF FUNCTIONS:"<<endl


<< " "<<endl
<< " ***MASS CONVERSION*** "<<endl
<< " "<<endl

<<"1. kilogram to pounds"<<endl
<<"2. pounds to kilogram"<<endl
<<"3. grams to kilograms "<< endl
<<"4. kilograms to grams"<< endl
<<"5. grams to milligrams"<<endl
<<"6. milligrams to grams "<<endl
<<"7. grams to ounces " <<endl
<<"8. ounces to grams " <<endl
<<"9. kilogram to Ounce"<<endl
<<"10. Ounce to kilogram "<<endl
<<"11. kilogram to tons "<<endl
<<"12. tons to kilogram "<<endl



cout<<" "<<endl
<<" "<<endl;

//SELECTION OF OPERATION TO BE USED
cout<<"Enter the NUMBER of the chosen Operation to be used: "<<endl
<<" "<<endl;
cin >> conversion;


//kilogram to pounds
if (conversion == 1)
{
cout << "Enter the numerical value in KILOGRAMS: " << endl;
cin >> kg;
pounds = kg/2.2046;
cout << " " << endl;
cout << "Kilograms in POUNDS:" << endl;
cout << pounds<< endl;
}

//pounds to kilogram
else if (conversion == 2)
{
cout << "Enter numerical value in POUNDS:" << endl;
cin >> pounds;
kg = pounds*2.2046;
cout << " " << endl;
cout << "Pounds in KILOGRAMS:" << endl;
cout << kg << endl;

}

//grams TO kilograms
else if (conversion == 3)
{
cout << "Enter numerical value in GRAMS:" << endl;
cin >> grams;
kg= grams /1000;
cout << " " << endl;
cout << "Kilograms in KILOGRAMS:"<< endl;
cout << grams<< endl;
}

//kilograms to grams
else if (conversion == 4)
{
cout << "Enter numerical value in KILOGRAMS:" << endl;
cin >> kg;
grams = kg*1000;
cout << " " << endl;
cout << "Kilograms in GRAMS:" << endl;
cout << grams<< endl;
}

// grams to milligrams
else if (conversion == 5)
{
cout << "Enter numerical value in GRAMS:" << endl;
cin >> grams;
mg = grams*1000;
cout << " " << endl;
cout << "Grams in MILLIGRAMS:" << endl;
cout << mg<< endl;
}

// milli to grams
else if (conversion == 6)
{
cout << "Enter numerical value in MILLIGRAMS:" << endl;
cin >> mg;
grams = mg/1000;
cout << " " << endl;
cout << "Milligrams in GRAMS:" << endl;
cout << grams<< endl;
}
// grams to ounces
else if (conversion == 7)
{
cout << "Enter numerical value in GRAMS:" << endl;
cin >> grams;
Oz = grams/28.34952;
cout << " " << endl;
cout << "Grams in OUNCES:" << endl;
cout << Oz<< endl;
}
// ounces to grams
else if (conversion == 8)
{
cout << "Enter numerical value in OUNCES:" << endl;
cin >> Oz;
grams = Oz*28.34952;
cout << " " << endl;
cout << " Ounces in GRAMS:" << endl;
cout << grams<< endl;
}

//kilogram to OZ
else if (conversion == 9)
{
cout << "Enter numerical value in KILOGRAMS:" << endl;
cin >> kg;
Oz = kg / 0.02834952;
cout << " " << endl;
cout << "Kilograms in OUNCES:" << endl;
cout << Oz<< endl;
}

// ounces to kilograms
else if (conversion == 10)
{
cout << "Enter numerical value in OUNCES:" << endl;
cin >> Oz;
kg = Oz*0.02834952;
cout << " " << endl;
cout << " Ounces in KILOGRAMS:" << endl;
cout << kg<< endl;
}
// kilogram to tons
else if (conversion == 11)
{
cout << "Enter numerical value in KILOGRAMS:" << endl;
cin >> kg;
Tons = kg/1000;
cout << " " << endl;
cout << "Kilograms in TONS:" << endl;
cout << Tons<< endl;
}
// tons to kilograms
else if (conversion == 12)
{
cout << "Enter numerical value in TONS:" << endl;
cin >> Tons;
kg = Tons*1000;
cout << " " << endl;
cout << " Ounces in KILOGRAMS:" << endl;
cout << kg<< endl;
}



//DEFAULT

else {
cout<<"Invalid input of Operations";
}

return 0;
}



please use code tags, its the <> thing on the editor.

you should read a tutorial on functions, but basically they work like the term function would in math.
y = f(x).
in c++,
y is the result that you wanted, and f(x) is a chunk of code that generates the result with an input.
the format is
type name (type parameter, type2 parameter2, ...)
or you can have nothing returned, and or nothing passed.
nothing returned, type of the function is void, nothing passed is empty ()
for example
double sum(double a, double b)
{
return a+b;
}
or
void printhello()
{
cout <<"hello";
}


1
2
3
4
5
6
7
8
9
10
void kilo2ton()
{
double kg;  //have to define these values locally, in scope.
cout << "Enter numerical value in KILOGRAMS:" << endl;
cin >> kg;
double Tons = kg/1000.0; //important to have that .0 to avoid integer math problems
cout << " " << endl;
cout << "Kilograms in TONS:" << endl;
cout << Tons<< endl;
}


Or perhaps:
1
2
3
4
5
6
7
8
9
10
void convert( const string& from, const string& to, double ratio )
{
  double in;
  cout << "Enter numerical value in " << from << " :\n";
  cin >> in;
  double out = in * ratio;
  cout << " \n";
  cout << from << " in " << to << ":\n";
  cout << out << '\n';
}
I'll suggest a different function. The code for each conversion is almost identical except for:
- The name of the input unit
- The name of the output unit
- The conversion factor

You could write a single function to handle them all. Use your code to create the convert() function below. See how it's called in main()
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
96
97
98
99
100
101
102
103
104
105
106
//UNIVERT
#include <iostream>
using namespace std;

// Convert from "fromUnit" to "toUnit". using toVal = fromVal * conversionFactor
void convert(const char *fromUnit, const char *toUnit, double conversionFactor)
{
        //  WRITE THE CODE HERE
}

int
main()
{
//VARIABLES
    int conversion;
    cout << "*******************Welcome to UNIVERT*******************" << endl;
    cout << " " << endl;
    cout << "***************UNIVERSAL UNIT CONVERTER*******************" << endl;
    cout << " " << endl;

    cout <<
	"**THE HARDER YOU WORK FOR SOMETHING, THE GREATER YOU'LL FEEL WHEN YOU ACHIEVE IT**"
	<< endl;
    cout << " " << endl;

//OPERATIONS
    cout << "LIST OF FUNCTIONS:" << endl
	<< " " << endl
	<< " ***MASS CONVERSION*** " << endl
	<< " " << endl
	<< "1. kilogram to pounds" << endl
	<< "2. pounds to kilogram" << endl
	<< "3. grams to kilograms " << endl
	<< "4. kilograms to grams" << endl
	<< "5. grams to milligrams" << endl
	<< "6. milligrams to grams " << endl
	<< "7. grams to ounces " << endl
	<< "8. ounces to grams " << endl
	<< "9. kilogram to Ounce" << endl
	<< "10. Ounce to kilogram " << endl
	<< "11. kilogram to tons " << endl
	 << "12. tons to kilogram " << endl;
    cout << " " << endl << " " << endl;

//SELECTION OF OPERATION TO BE USED
    cout << "Enter the NUMBER of the chosen Operation to be used: " << endl
	<< " " << endl;
    cin >> conversion;

//kilogram to pounds
    if (conversion == 1) {
	convert("kilograms", "pounds", 1/2.2046);
    }
//pounds to kilogram
    else if (conversion == 2) {
	convert ("pounds", "kilograms", 2.2046);
    }
//grams TO kilograms
    else if (conversion == 3) {
	convert("grams", "kilograms", 0.001);
    }
//kilograms to grams
    else if (conversion == 4) {
	convert("kilograms", "grams", 1000);
    }
// grams to milligrams
    else if (conversion == 5) {
	convert("grams", "milligrams", 1000);
    }
// milli to grams
    else if (conversion == 6) {
	convert("milligrams", "grams", 0.001);
    }
// grams to ounces
    else if (conversion == 7) {
	convert("grams","ounces",1/28.34952);
    }
// ounces to grams
    else if (conversion == 8) {
	convert("ounces", "grams", 28.34952);
    }
//kilogram to OZ
    else if (conversion == 9) {
	convert("kilograms","ounces",1/0.02834952);
    }
// ounces to kilograms
    else if (conversion == 10) {
	convert("ounces","kilograms",0.02834952);
    }
// kilogram to tons
    else if (conversion == 11) {
	convert("kilograms","tons",0.001);
    }
// tons to kilograms
    else if (conversion == 12) {
	convert("tons","kilograms", 1000);
    }

//DEFAULT

    else {
	cout << "Invalid input of Operations";
    }

    return 0;
}


Topic archived. No new replies allowed.