Hello CornFlakes123,
Lets look at your second program:
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
|
#include <iostream>
#include <limits> // <--- Added.
#include <string>
using namespace std; // <--- Best not to use.
double getWeight();
double averageWeight(double weightOne, double weightTwo, double weightThree);
int main()
{
double weight1{}, weight2{}, weight3{}, average{}; // <--- Always initialize your variables.
weight1 = getWeight();
weight2 = getWeight();
weight3 = getWeight();
average = averageWeight(weight1, weight2, weight3);
cout << "The patient's average weight is: " << average << " lbs" << endl;
//system("pause");
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
double getWeight()
{
double weightOne1 = 0.00;
double weightTwo2 = 0.00;
double weightThree3 = 0.00;
cout << "Please enter a weight: ";
cin >> weightOne1;
cout << "Please enter a weight: ";
cin >> weightTwo2;
cout << "Please enter a weight: ";
cin >> weightThree3;
return weightOne1, weightTwo2, weightThree3; // <--- Can return only 1 of the 3 and most likely returning the value of the last 1.
}
double averageWeight(const double weightOne, const double weightTwo, const double weightThree) // <--- Made constant because they should not be changed.
{
double average = 0.00;
average = (weightOne + weightTwo + weightThree) / 3.00;
return average;
}
|
Your code with some comments and changes.
Prefer to use "double" over "float".
You program is doing exactly what you told it to do.
When you say:
"Please enter a weight: " 9 times. It should be 3 times. What's the problem?
|
The program is doing what you told it to do and (9) times is expected.
A function should do 1 thing and do it well then return something
if needed. Next a function can only return 1 thing. In your program that should be the weight that is entered. Asking for 3 weights and calling the function 3 times is where you (9) is coming from.
An alternative would be to pass the 3 variables by reference then you would not have to deal with a return value.
Compare your function call to what your function is doing. This may help
https://www.learncpp.com/cpp-tutorial/introduction-to-functions/
Also compare the "getWeight" function to the "averageWeight" function which is properly coded.
Your "getWeight" function is over done. It only needs to be asking for 1 weight and returning its value. That is why you have 3 function calls.
One last note: A prototype may only need the variable type, but copying the function definition with the variable names can help when writing a function call if your IDE supports this.
Andy