So, I am looking to create a program that finds the smallest of the 2 integers that the user inputs. This is what I have, but I must of made a logic error somewhere. The program runs fine, however, instead of finding the smallest of the two integers the program outputs the first integer that the user inputs. I just want to know where I am going wrong. Thanks
# include <iostream>
# include <cmath>
# include <iomanip>
# include <cstdlib>
# include <ctime>
int smaller (int number1, int number2)
{
int result;
if (number1 < number2)
result = number1;
else
result = number2;
return result;
}
int main ()
{
usingnamespace std;
int a;
int b;
cout << "Enter two integers" << endl;
cin >> a, b;
int c = smaller(a,b);
cout << "The smaller number is" << endl;
cout << c << endl;
okay, this is what is going on. When you set the int smaller function you declared it as an integer. An integer can be more than a single character. Here is your new code.
# include <iostream>
usingnamespace std;
char smaller (int number1, int number2)
{
int result;
if (number1 < number2)
result = number1;
else
result = number2;
return result;
}
void main ()
{
char a, b, c;
cout << "Enter two integers" << endl;
cin >> a >> b;
cout << "The smaller number is" << endl;
c = smaller(a,b);
cout << c << endl;
}