The question that I am attempting to work out is as follows :
"A government research lab has concluded that an artificial sweetener commonly used in diet soda pop
will cause death in lab mice. A friend of yours is desperate to lose weight but can't give up soda pop.
Your friend wants to know how much soda pop it's 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 of 0.001.
Your program should allow the calculation to be repeated as often as the user wishes."
I have completed the first draft of the code which is attached. My question is :
1) Is the logic of the program correct ? For example the cin values ;
Yields the value '104000' for the variable (DietSodaPopCans); meaning that it takes 104000 cans
to kill the dieter at his/her given dieting weight. Does it sound sensible ? I feel that the two
equations written on the C++ code is logical. But I would need some checking on that please.
2) The question above requires that the program be repeated as many times as the user wishes. For this,
what type of looping mechanism could I use ? Would a while looping suffice or should I go for
do...while looping ?
[b]The completed C++ Source code is as shown below :[/b]
//Author: Andy8
//Soda Pop Death
//Date: 23 August 2011
cout << "This program calculates how many cans of soda it will take to kill you !\n";
cout << "Each can contains 0.001 (0.1%) of artificial sweetener\n" << endl;
cout << "Enter the amount of Artificial Sweetner needed to kill a mouse: \n";
cin >> SweetnerMouse;
cout << "Enter the weight of the mouse in grams: \n";
cin >> WeightMouse;
cout << "Enter the weight of the dieter in grams at which dieting activity will be stopped: \n";
cin >> WeightDieter;
Yes, it's right. But it doesn't take 104000 cans to kill the dieter, it takes 10400 grams. There's no unit conversion from grams to cans. As for the loop, either a while or a do...while loop will work. Just remember that if you use a while loop, the condition will need to be met for the first iteration, so you'd need to initialize the answer variable to a positive answer before the loop, i.e.:
1 2 3 4 5 6 7
std::string answer = "yes";
while (answer == "yes")
{
// Code to be looped
std::cout << "Calculate again? ";
std::cin >> answer;
}
A do...while loop might be preferable just for the sake of readability though.