Reference Function Returning Garbage Value

Hello, I'm able to get my code to compile, however my void function is returning an output of 0, 0, 32766 no matter what the integer input is. I'm supposed to sort integers input by the user from lowest to highest, I can not use an array. The void function is supposed to pass the ints input by the user as reference parameters. Where is my error? Thank you!

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
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;

void smallSort(int &a, int &b, int &c);

int main()
	{
	int num1;
	int num2;
	int num3;
	int a;
	int b;
	int c;

	cout << "Please enter three integers and they will \n"
	     << "display from lowest to highest value. \n";
	cin >> num1 >> num2 >> num3;
	smallSort(num1, num2, num3);
	cout << " " << a << " " << b << " " << c << endl;

	return 0;
	}

void smallSort(int &a, int &b, int &c)
	{
	int num1;
	int num2;
	int num3;
	int temp;

	a = num1;
	b = num2;
	c = num3;
	if (a > b)
		{
		temp = a;
		a = b;
		b = temp;
		}
	if (a > c)
		{
		temp = a;
		a = c;
		c = temp;
		}
	if (b > c)
		{
		temp = b;
		b = c;
		c = temp;
		}
	}
Lines 14-16: a,b,c are uninitialized variables (garbage).

Line 22: You're outputting garbage.

Lines 29-31: num1, num2, num3 are uninitialized variables (garbage).

Line 34-36: You're assigning garbage to a, b and c.

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
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;

void smallSort(int &a, int &b, int &c);

int main()
{   int a;
	int b;
	int c;

	cout << "Please enter three integers and they will \n"
	     << "display from lowest to highest value. \n";
	cin >> a >> b >> c;
	smallSort(a, b, c);
	cout << " " << a << " " << b << " " << c << endl;

	return 0;
	}

void smallSort(int &a, int &b, int &c)
{   int temp;

    if (a > b)
	{   temp = a;
		a = b;
		b = temp;
	}
	if (a > c)
	{   temp = a;
		a = c;
		c = temp;
	}
	if (b > c)
	{   temp = b;
		b = c;
		c = temp;
	}
}
Last edited on
Topic archived. No new replies allowed.