Please help to fix my code

Hi , I wrote a code (with this website's help) for conversion pounds to kilograms and kilograms to pounds. The problem is that it converts pounds to kilograms but not kilograms to pounds
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
 //Program
#include <iostream>
using namespace std;

void weightInput (double &kilograms, double &pounds)
{
cout<< "Please enter weight in pounds :): " ;
cin>> pounds;
cout<< "Please enter weight in kilograms :): " ;
cin>> kilograms;
}

void weightCalculating (double &kilograms, double &pounds)
{
kilograms = pounds / 2.2046;
pounds = kilograms * 2.2046;
}

void weightOutput (double &kilograms, double &pounds)
{
   cout<< kilograms << "kilograms and " << pounds << "pounds.\n\n";
}
int main()
{
    double kilograms;
    double pounds;
   char userInput;
   do {
    weightInput (pounds, kilograms);
    weightCalculating (kilograms, pounds);
    weightOutput (kilograms, pounds);
    cout<< "Would you like to do another conversion?\n Y for yes or any other key to quit.";
    cin>> userInput;
   }while (userInput == 'Y');
    return (0);
}


    
Last edited on
Hello abumirza,

You should do some research on the formulas to convert pounds to kilograms and kilograms to pounds. Both your formulas in "weightCalculating" are wrong. In "weightOutput" it prints the correct variables, but they have the wrong answers.

I would have set up the program different allowing the user to choose which way to convert and do each conversion separate,

Hope that helps,

Andy
Hey. As Andy mentioned. You should do some research into the formulas, I have them linked in the code below.

I re-wrote your program real quick so it makes a bit more sense, and looks a bit better. You can ask any questions, or make it your own somehow -

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
#include <iostream>

using namespace std;

void kiloToPounds(double& kiloToPounds)
{	
	cout << kiloToPounds << " Kg in pounds is: ";
	kiloToPounds=kiloToPounds/0.45359;
       // http://www.rapidtables.com/convert/weight/how-kg-to-pound.htm
     
	cout << kiloToPounds << " pounds" << endl;
}

void poundsToKilo(double& poundsToKilo)
{
	cout << poundsToKilo << " pounds in kg is: ";
	poundsToKilo = poundsToKilo * 0.45359; // http://www.rapidtables.com/convert/weight/how-pound-to-kg.htm
	cout << poundsToKilo << "kg" << endl;
}

void weightInput()
{
	cout << "Enter 1 for Kilo to pound conversion. Enter 2 for pound to Kilo conversion: ";
	int choice;
	cin >> choice;

	cout << "\n\nEnter the weight: ";
	double weight;
	cin >> weight;

	if (choice == 1)
	{
		kiloToPounds(weight);
	}
	else if (choice == 2)
	{
		poundsToKilo(weight);
	}
}

int main()
{
    double kiloToPounds;
    double poundsToKilo;
	char userInput;

   do 
   {
	   weightInput();
	 
    cout<< "Would you like to do another conversion?\n Y for yes or any other key to quit.";
    cin>> userInput;

   } while (userInput == 'Y' ||userInput == 'y'); // this picks up either y or Y.

    return (0);
}

Last edited on
Thank you Andy. Thanks a lot TarikNeaj. TarikNeaj, your code works perfect and looks so nice. When I try to write code , there is always something wrong and it looks ugly. How do I improve ?
You're welcome.

It's something you just learn overtime. When I was were you are, my code was also a mess. Keep writing, keep learning. You will get there, good job so far :)

hello abumirza,

As I worked with your program I found another problem. With your code

1
2
3
4
5
void weightCalculating(double &kilograms, double &pounds)
{
	kilograms = pounds * 0.45359237;
	pounds = kilograms * 2.2046;
}

you pass "kilograms" and "pounds" by reference which allows the variables in main to be changes. The problem is that line 3 will change the value of "kilograms" both in the function and in main. then when you use "kilograms" in line 4 it has the wrong value. A good point to make two separate functions that would return a value so as not to change your original input.

Hope that helps,

Andy
By the way: This
1
2
kilograms = pounds / 2.2046;
pounds = kilograms * 2.2046;
is the correct formula just used wrongfully.

How do I improve ?
Learn to use the debugger. Going step by step through your code will show you what really happens.
its not only the pass by ref, its that pounds uses kilos which was just modified.
he could still pass by ref to populate both values, but needs a temp...


t = p * 0.4;
p = k* 2.2;
k = t;

Personally, I would prefer 2 constants and be done with it.


#define k2p 2.2
#define p2k 0.45

...
x = k2p* value;

Topic archived. No new replies allowed.