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>
usingnamespace 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);
}
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
#include <iostream>
#include <algorithm>
usingnamespace 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
#include <iostream>
#include <algorithm>
usingnamespace 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
}