return 3 values

could you tell me, may I do this in this way?
I mean in functions to return 3 values

1
2
3
4
5
6
7
8
char min(char x, char y, char z)
{
	
	if(y<x && y<z)return y;
	if (z<x && z<y) return z;
	if( x<y && x<z ) return x;
		
}

this is my programm:
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
#include <iostream>
#include <string>
using namespace std;
int min(int , int, int);
double min(double, double, double);
char min(char, char, char);
string min(string , string, string);
int main() {
	int a(1), b(2), c(3);
	double e=1.5, f=2.5, g=3.5;
	char h='D', i='F', j='Z';
	string k="firstname", l="surname", m="name";
	cout<<"minimum of "<<a<<' '<<b<<' '<<c<<" is "<<min(a, b, c)<<endl;
	cout<<"minimum of "<<e<<' '<<f<<' '<<g<<" is "<<min(e, f, g)<<endl;
	cout<<"minimum of "<<h<<' '<<i<<' '<<j<<" is "<<min(h, i, j)<<endl;
	cout<<"minimum of "<<k<<' '<<l<<' '<<m<<" is "<<min(k, l, m)<<endl;

	system ("pause");
	return 0;

}
int min(int x, int y, int z)
{
	
	if(y<x && y<z)return y;
	if (z<x && z<y) return z;
	if( x<y && x<z ) return x;
	
	
}
double min(double x, double y, double z)
{
	
	if(y<x && y<z)return y;
	if (z<x && z<y) return z;
	if( x<y && x<z ) return x;
	
	
}
char min(char x, char y, char z)
{
	
	if(y<x && y<z)return y;
	if (z<x && z<y) return z;
	if( x<y && x<z ) return x;
	
	
}
string min(string x, string y, string z)
{
	
	if(y<x && y<z)return y;
	if (z<x && z<y) return z;
	if( x<y && x<z ) return x;
	
	
}


I'm new in this Forum, please help me!!
That's one way of doing it, although you might get a compiler error as the function would return nothing if the 2 lowest values are equal. Try using "<=" instead of "<".
I would use ausairman's suggestion and also suggest you always use "else if" statements in these cases for readability:

1
2
3
4
5
6
7
8
9
int min(int x, int y, int z)
{
   if (y <= x && y <= z)
      return y;
   else if (z <= x && z <= y) 
      return z;
   else if (x <= y && x <= z ) 
      return x;
}
Last edited on
Thank you!! :))
Last edited on
compound if statements are ugly, IMO. I find something like this to be easier to follow and less error prone:

1
2
3
4
5
6
7
8
9
int min(int x, int y, int z)
{
   if(y < x)
      x = y;
   if(z < x)
      x = z;

   return x;
}
Topic archived. No new replies allowed.