#include <iostream>
/*
max3.cpp -- program to print out the largest of three input values
usage: ./max3
input: three doubles
output: the largest of the three input values
note: no prompt for user, no explanation on output
modified by: David Kim
modified on: date of completion
*/
usingnamespace std;
double max3(double, double, double); // tell compiler about this
int main()
{
double a,b,c; // input values
double largest; // result
cout << "Please enter three numbers.";
cin >> a >> b >> c; // read in three values
largest = max3(a,b,c); // compute the largest
cout << largest << endl; // display it on the screen
}
double max3(double n1, double n2, double n3) //where MY code begins
{
if ( n1 > n2 && n1 > n3)
{
cout << n1 << endl;
}
elseif ( n2 > n1 && n2 > n3)
{
cout << n2 << endl;
}
elseif ( n3 > n1 && n3 > n1)
{
cout << n3 << endl;
}
return 0;
}
So basically what I'm trying to do is find the largest number and at the moment it's kind of working but when I input the three doubles and hit return I get the correct answer and on the line below a "0". Why is that?
Also, the first half my professor made can someone help me through with the purpose of that top half. Mind you this is my first course ever in computer science and It's only the third week in so I'm still kind of lost as the professor skipped over the more basics of syntax and whatnot.
When you call the function max3, it outputs the max value and at the end of the function returns 0. You'd be better off changing your cout statements in the if and else if statements to return n1 , n2, or n3, and not have the return 0 at the end of the function
i will not explain everything in detail, you can figure it out by yourself just b doing it.
cout prints data on the screen.
<< and >> are operators which is way to advanced to discuss right now.
cin lets you capture data enter via the keyboard.
so , or example, enter this when the program runs
1<enter>2<enter>3<enter>
this will result in:
a : 1
b : 2
c : 3
now you need to determine which is the largest, a b or c.
max3 is a function where a b and c are passed to.
n1 will get vallue of a, n2 will get b and n3 will get c.
in your code you are printing the result.
But you teacher wants you to return the largest value back to the main and there it will be stored in the varaible "largest".
and this variable is shown on the screen.
which, in your case, is always 0, because you return 0 at the end of your function.
you should return the max number and delete all the cout lines in your own code.
Yeah wow Jikax thanks a lot! That really does clear up things. And so because I have "return 0;" in my code all that function is doing is kind of nothing because it'll always return "0" back to the main which will then spit out "0" as the largest value.
That error "control reaches end of non-void function" means you do not have a return value if none of your control statements get activated. Even though you programmed it so that one of the three will always run, your compiler does not know that. Simply leave the code as jb has given you and just add a return -1; or some number to the end of the code so the compiler is satisfied.
However, you should look at what happens when 2 numbers are the same... What do you want to happen?
And you got a little error in your last else if statement;)