Programming Project #1
Well, my first official college programming project has been turned in.
A government research lab has concluded that an artificial sweetener commonly used in diet soda pop will cause death in laboratory mice. A friend of yours is desperate to lose weight, but cannot give up soda pop. Your friend wants to know how much diet soda pop it is possible to drink without dying as a result. Write a program to supply the answer. The input to the program is the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, and the weight of the dieter. To ensure the safety of your friend, be sure the program requests the weight at which the dieter will stop dieting, rather than the dieter’s current weight. Assume that diet soda contains 1/10th of 1% artificial sweetener. Use a variable declaration with the modifier const to give a name to this fraction. You may want to express the percent as the double value 0.001. Your program should allow the calculation to be repeated as often as the user wishes.
The project outline was really vague and gives a great lesson on project specifications. The required information was not present in the outline, information such as how is the diet soda measured? I resorted to measuring everything in ounces, which is awkward when measuring a liquid such as diet soda (the measurement is expected to be in fluid ounces, but in this case must be in mass form).
Below, I will highlight a few of the more interesting pieces of code.
do{
cout << mouse_weight;
if(mouse_weight <= 0)
cout << "Input must be greater than 0."
<< endl << endl;
}while(mouse_weight <= 0);
In this loop, I prompt for the weight of the mouse and continue to test until I get a valid result (mice don’t usually have a negative weight). This loop is not perfect, when it receives unexpected input, it goes into an infinite loop:
Weight of the mouse (in ounces): a
Input must be greater than 0.
Weight of the mouse (in ounces): Input must be greater than 0.
Weight of the mouse (in ounces): Input must be greater than 0.
Weight of the mouse (in ounces): Input must be greater than 0.
Weight of the mouse (in ounces): Input must be greater than 0.
In Project #2, I added a function to do all of this work for me; essentially, I call get_positive_double() and it returns a double while handling erroneous input (negatives, zero, and characters), plus it looks a whole lot cleaner. Unfortunately, I can’t show you this yet because the project isn’t due for a few weeks. Check back then to approximate e^x and to see how many servings of your favorite food it takes to maintain your current weight.
Click here to download the full source code.