Weight Conversion Program

My assignment is to write a program that will convert from pounds and ounces to kilograms and grams. My teacher wants us to make two global constants for the conversion multiples (these are the only global variables that are allowed). A pound is 453.6 grams. An ounce is 28.3495 grams. There are 1000 grams in a kilogram.
I am stuck on the conversion and output process. I don't understand how to call a user input in order to make an equation. My beginner's knowledge wants me to try and write something like pounds = pounds_g * ch and put that in the conversion function and go from there but I know that is completely wrong. Am I supposed to create new local variables for each new function or will that mess things up and make it more complicated? How am I supposed to convert and output using these functions?

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

double pounds_g = 453.6;
double oz_g = 28.3;

int promptAndGetInt(string msg);
int convertToGrams();
int outputResults();

int main() {
	int pounds, ounces, grams;

	cout << "  ~ wELcOmE tO tHe wEigHt cOnVeRtEr pRoGrAm ~\n"
		"\nThis program will convert pounds and ounces to kilgrams and grams\n"
		"First I will prompt you for number of pounds and then the number of ounces\n";

	cout << promptAndGetInt("\nPounds: ") << endl;

	cout << promptAndGetInt("\nOunces: ") << endl; 
	

	system("pause");
	return 0;
}

int promptAndGetInt(string msg) {
	cout << msg;
	char ch;
	cin >> ch;
	while (isdigit(ch) == false) // cctypelib
		cin >> ch;
	cin.putback(ch);
	int response;
	cin >> response;
	return response;
}

int convertToGrams() {
	
	return 0;
}
int outputResults() {

	return 0;
}
Last edited on
Hello Cplusplusnubblub,

Looking at your program line 14 defines three variables that contain an unknown value, i.e., garbage and are never used.

This is followed by three "cout" statements the second and third may just print a number,but "pounds" and "ounces" should be set equal to the returned result.

Your function to get input may work, I have not tried it yet, but there C++ ways of doing the same thing.

You need to initialize you variables in main. Store the right information in "pounds" and "ounces" and send these along with "grams" to the conversion function.

The use of "isdigit" will need the header file "<cctype>". Not the "cctypelib" that is in your comment.

Hope this helps,

Andy
Hello Cplusplusnubblub,

I believe there may be enough of a time difference between us to make working on this long and drawn out, so I will try to get ahead.

My teacher wants us to make two global constants for the conversion multiples (these are the only global
variables that are allowed)
A pound is 453.6 grams. An ounce is 28.3495 grams. There are 1000 grams in a kilogram.

You have covered this well.

The proto types are in the right place, but need revised.

In main, after working with the program, these variables are better defined as type double. And you should always initialize your variables when they are defined. Not a big problem with your program, but could be. Later when you convert "pounds" and "ounces" it is better to work with doubles because using ints will loose information.

The first "cout" is OK, but I added Whole numbers please\n after the last "\n" to prepare for the "promptAndGetInt" function.

The next two "cout" statements will only print out the number entered not store it in the variables where they are needed. These need to be function calls that make use of what is returned.

You need two more function calls to the "convertToGrams" and "outputResults" functions both will need parameters between the () to work.

The "promptAndGetInt" function works, but not well. If you enter anything other than a whole number for "pounds" you will not get the correct number wanted and "ounces" will receive whatever is left in the input buffer. There are better ways using C++ to do this function.

My beginner's knowledge wants me to try and write something like pounds = pounds_g * ch and put that
in the conversion function and go from there but I know that is completely wrong.

Yes that is completely wrong. First you do not want to put the answer into "pounds", but something else. This will be a temporary variable used in the function. The name should reflect that it is the result of changing pounds to grams and be a separate variable from converting ounces to grams. Both of which are added together to get total grams. Each using the appropriate constant defined at the beginning of the program. Once you have worked out the conversion for pounds and ounces add the together and return the result.

In the "outputResults" function I sent all three variables from main and used them in one "cout" statement to print the results.

That should give you something to work on for awhile.

Hope that helps,

Andy
Thanks for the help, I eventually got my code to look like this



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 "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

double pounds_g = 453.6;
double oz_g = 28.3495;

int promptAndGetInt(string msg);
int convertToGrams(int pounds, int ounces);
void outputResults(int pounds, int ounces, int grams);

int main() {
	int pounds, ounces, grams;

	cout << "  ~ wELcOmE tO tHe wEigHt cOnVeRtEr pRoGrAm ~\n"
		"\nThis program will convert pounds and ounces to kilgrams and grams\n"
		"First I will prompt you for number of pounds and then the number of ounces\n";

	pounds = promptAndGetInt("\nPounds: ");
	ounces = promptAndGetInt("\nOunces: ");
	cout << "\nRESULTS:\n" << endl;
	grams = convertToGrams(pounds, ounces);
	outputResults(pounds, ounces, grams);

	system("pause");
	return 0;
}

int promptAndGetInt(string msg) {
	cout << msg;
	char ch;
	cin >> ch;
	while (isdigit(ch) == false)// cctypelib
		cin >> ch;
	cin.putback(ch);
	int response;
	cin >> response;
	return response;
}

int convertToGrams(int pounds, int ounces) {


	return pounds*pounds_g + ounces*oz_g + .5;
}

void outputResults(int pounds, int ounces, int grams)
{
	cout << "\nPounds: " << pounds << endl;
	cout << "Ounces: " << ounces << endl;
	cout << "Grams: " << grams % 1000 << endl;
	cout << "Kilograms: " << grams / 1000 << endl;
}
/*
~ wELcOmE tO tHe wEigHt cOnVeRtEr pRoGrAm ~

This program will convert pounds and ounces to kilgrams and grams
First I will prompt you for number of pounds and then the number of ounces

Pounds: 5

Ounces: 4

RESULTS:
Pounds: 5
Ounces: 4
Grams: 381
Kilograms: 2
Press any key to continue . . .

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~ wELcOmE tO tHe wEigHt cOnVeRtEr pRoGrAm ~

This program will convert pounds and ounces to kilgrams and grams
First I will prompt you for number of pounds and then the number of ounces

Pounds: 13

Ounces: 11

RESULTS:
Pounds: 13
Ounces: 11
Grams: 209
Kilograms: 6
Press any key to continue . . .

*/




Hello Cplusplusnubblub,

Sorry I lost track of this post for awhile.

Your math is wrong and the out put is wrong giving you the wrong answers.

You are doing integer multiplication and division with floating point numbers. This drops the decimal portion of the number and affects the calculation.

In the " outputResults" function: What is the point of cout << "Grams: " << grams % 1000 << endl;? Why is "% 1000" needed? It gives you the wrong output.

Compare your output:

This program will convert pounds and ounces to kilgrams and grams
First I will prompt you for number of pounds and then the number of ounces

Pounds: 5

Ounces: 4

RESULTS:
Pounds: 5
Ounces: 4
Grams: 381
Kilograms: 2
Press any key to continue . . .


To my output:

  ~ wELcOmE tO tHe wEigHt cOnVeRtEr pRoGrAm ~

 This program will convert pounds and ounces to kilgrams and grams
 First I will prompt you for number of pounds and then the number of ounces
 Whole numbers please

Pounds: 5

Ounces: 4


 5 pounds and 4 ounces converts to 2,381.2 grams or 2.3812 kilograms




 Press any key to continue


This is the difference of using a double for "grams" over the "int" you used. At the least "grams" should be a double. It would not hurt if everything defined as an "int" was changed to a "double".

It is a good program, but still could use some work to get the correct output.

Hope that helps,

Andy
Topic archived. No new replies allowed.