Displaying the lowest number

I'm trying to input three numbers then return the smallest number. The program will not compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int smallest(int x, int y, int z){

int smallest = min(x,y);

if (x < smallest)
smallest=x;
if (y < smallest)
smallest=y;
if(z < smallest)
smallest=z;

return min(smallest,z);
}
If this is the whole program u'r missing main()

I'd also refrain from having a function called with the same name as a variable.
1
2
int smallest(int, int, int);
int smallest;


Here:
1
2
3
4
5
6
7
8
9
10
int Smallest(int x, int y, int z)
{
int least = x;
if(y < least)
   least = y;
if(z < least)
   least = z;

return least;
}
Last edited on
Have you done this:

#include <algorithm>

Without this the compiler won't know "min"!

Edit said: The code posted by soranz is much more elegant!
Last edited on
I'm confused on soranz's code. Am I modifying it to look like the first set of code or am I changing it to the second set of code.
Change it to the 2nd set.
Am I modifying it to look like the first set of code or am I changing it to the second set of code.


I'm only trying to say that there are many reasons why this naming style should not be done:
1
2
int smallest(int, int, int);
int smallest;


and should be changed in favor of something like:
1
2
3
4
int SmallestNum(int, int, int); //all functions start with a capital letter
int iMinNum; //prefix all integer variables with 'i'
bool bMinNum //prefix all bool with 'b'
//etc 


I honestly don't know what I'm doing wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	 
int SmallestNum(int x, int y, int z){
	      
int smallest = min(x,y);
	      
if (x < smallest)
smallest=x;
if (y < smallest)
smallest=y;
	      
return min(smallest,z);
}

return 0;
}
it looks like your placing the function definition inside of the main program. It should be after the end of the program. Inside of MAIN should be be a call to the function, with 3 integer numbers that you intend to use.

you will also need function prototype before main.

I am just a rookie as well, but just an idea.

Protoype
 
int SmallestNum(int, int, int);


Call to function

 
SmallestNum(5,27,12);  // or whatever numbers you are using 
Last edited on
SmallestNum is a function. Functions cannot be declared/defined within another function in c++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
using namespace std;

//function prototype that matches the definition
int SmallestNum(int x, int y, int z);

int main()
{
int a = 5;
int b = 9;
int c = 7;

int result = SmallestNum(a, b, c); //call the function SmallestNum
cout << "!!!the smallest number is!!!   " << result ;
return 0;
}

//function definition of SmallestNum
int SmallestNum(int x, int y, int z)
{	      
//code to determine smallest number goes here
}
Topic archived. No new replies allowed.