selecting minimum with negative numbers

I had to create a program which selects and outputs the lowest of three user input numbers using a funcation. It HAS to be in a function:

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
#include <iostream>

using namespace std;

double minimum(double p1, double p2, double p3) 
{
	if (p1 < p2 && p3)
		return p1;        
	if (p2 < p1 && p3)
		return p2;
	if (p3 < p1 && p2) 
		return p3;
}

int main()
{
	cout << "This program employs a function to determine the minimum of 3 numbers." << endl;
	cout << endl;
	
	double r1, r2, r3;
	cout << "Please enter your first value. ";
	cin >> r1;
	
	cout << "Please enter your second value. ";
	cin >> r2;
	
	cout << "Please enter your third value. ";
	cin >> r3;
	
	cout << "The minimum of " << r1 << ", " << r2 << ", and " << r3;
cout << " is " << minimum(r1, r2, r3)<< "." << endl; 

}


It works fine with all positive numbers but when negative numbers are introduced,

these numbers in particular:

-1029
7829
-9999

I haven't messed with too many negative numbers. But the code selects -1029 as the minimum number other than -9999.

Any help would be greatly appreciated.
if (p1 < p2 && p3) does not do what you think it does, see http://www.cplusplus.com/doc/tutorial/operators/
Logical operators don't work that way. You have to do this:

if (p1 < p2 && p1 < p3)
Take a look at this. Do you understand why the program doesn't work when you enter the same three values? Hint: it is related to the compiler warning that you are probably getting.
Please enter your first value. 5
Please enter your second value. 5
Please enter your third value. 5
The minimum of 5, 5, and 5 is 2.64188e-308.
I suggest reading some other threads. You'll be better off testing two values at a time and reading input in a loop. It is very similar to some of the threads were someone is looking for the max. The first one there had some complete examples that you could use as a guideline.
http://www.cplusplus.com/forum/general/21148/
http://www.cplusplus.com/forum/beginner/8076/
Topic archived. No new replies allowed.