find the smaller integer, logic error?

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

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
# 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 ()
{
   using namespace 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;
Last edited on
Can you cin 2 variables on the same line? I get a run-time error. It works just fine if you put cin >> a and cin >> b on two separate lines.
Last edited on
Compile with warnings.
warning: right operand of comma operator has no effect [-Wunused-value]
  cin >> a,b;


Also, learn to indent your code.
First thing I noticed was the cin statement. You can write it like this:
cin >> a >> b;
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.
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
# include <iostream>

using namespace 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;
}

Notice that it is very similar to your original.
Topic archived. No new replies allowed.