$24
Weight conversion program:
The purpose of this program is for you to understand how functions work, and practice using them.
Program Description: Your assignment is to write a program that will convert from pounds and ounces to kilograms and grams. Your program will first prompt the user for the number of pounds and then the number of ounces. Then your program should display the results.
You will want 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. Remember there are 1000 grams in a kilogram, so if the program has over 1000 grams, convert these 1000 grams to 1 kilogram. For your final output round your grams up to the nearest gram. For example: .5 grams would round up to 1 gram.
This program is a simple program where you will practice creating functions so you
are required to have these 3 different functions. If you want to create another function, you are free to, but you must have these three. NOTE: It is required that you use the function prototypes as shown below
Function Purpose
Prompt the user with the string argument and then safely (keep reading until the user gives you an integer) read an int, which is returned. The parameter prompt is a string that will tell the user what is wanted.
Convert from pounds and ounces to grams (First calculate grams as a double to get all the accuracy, and then round off to the nearest int (by adding .5 while it is still a double) and returning grams as a int)
Function Prototype
int promptAndGetInt( string promtMessage);
int convertToGrams( int pounds, int ounces);
Sample Function Call
answer = promptAndGetInt( "Number of Cats:" );
grams = convertToGrams( pounds, ounces );
Output the results to the console
window (the black window)
void outputResults(int
outputResults( pounds, ounces,
Note: the function will need to
pounds, int ounces,
grams );
convert grams to kilograms and
int grams );
grams
Here is an example of how your main should look:
int main(){
int pounds, ounces, grams;
cout << " Welcome message..........\n";
pounds = promptAndGetInt("Pounds: ");
ounces = promptAndGetInt("Ounces: ");
grams = convertToGrams(pounds, ounces);
outputResults(pounds, ounces, grams);
return 0;
}
Example Program
Turn in:
A paper copy of your source file (cpp), as well as, an example run with the data shown below.
Grading:
Remember to have the program documentation at the top of the program (for an example, you can look back at program 1 or 2).
Remember to put the function prototypes before main and the function definitions below main.
For each function definition you need to tell briefly what the function does (place description before the function definition).
Did you use the putback function so that you could safely read in the int (handle bad user input)?
Remember to staple your papers together
You will also lose points for not indenting the code of a body (code within { and } ) and other style issues (like proper naming of variables and proper capitalization of variables).
Program Input to use for Turn-In: (and answers so you can verify your output)
Pounds
Ounces
Answer:
Grams
Kilogram
5
4
2
381
13
11
6
209
A Note about Strings:
You will need to use the string data type to be able to write out the prompt for the promptAndGetInt function. We have not talked much about the string data type yet, but using strings are pretty easy, and I think you can understand how to use them from the examples given below. What is a string? It is 0 or more char's. For example, these could all be stored as strings: "hello", "123", "F-16" "hello there". Strings are defined in the <string library.
string str;
cin str;
// user types: hello there
cout << str;
// hello is displayed
Situation
Needed Action/ Function
Example
Write out a string variable
<<
cout << stringVarible
Pass a string variable to a
int exampleFunction( string
exampleFunction( "John
function
name );
Doe" );
Hints: Write the program a piece at a time. Write a few lines of code, and then test those lines of code. Use cout's to tell you about what is happening in the program. Ask me if you have questions...about the requirements or when programming.