Arrange 3 variables in descending order using swap

I'm having trouble with this project. I've been trying to get this code to run and have been greeted with the same "The system cannot find the file specified" error message each time. I'm still very new to C++ and would appreciate any help I can get with this.

I'm trying to get 3 user inputted values to be arranged in descending order.

Here's the original prompt if it helps:
"Write a class named Sort3 that holds three floating point values as member data and a mySort() member method. The mySort() method should sort the three member values from largest to smallest (descending order). The class must use references (or pointers) as mySort() has to affect (sort) the three variables entered by the user in the calling program (main)."

Thank you in advance.

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
  #include <iostream>
using namespace std;

class Sort3
{
private:
	float& first;
	float& second;
	float& third;

public:
	Sort3(float& a, float& b, float& c) : first(a), second(b), third(c)
	{
	}

	void swap(float& x, float& y)
	{
		float temp;

		temp = x;
		x = y;
		y = temp;
	}
	void mySort()
	{
		if (first < second)
			swap(first, second);
		if (third < second)
			swap(second, third);
		if (first < third)
			swap(first, third);
		return;
	};
	int main()
	{
		float first, second, third;
		cout << "Please enter three integers." << endl;
		cin >> first, second, third;
		Sort3 obj(first, second, third);
		obj.mySort;
		cout << first, second, third;
		return 0;
	}
};
Shouldn't you do

1
2
3
4
5
6
if(first < second)
    swap(first, second);
if(first < third)
    swap(first, third);
if(second < third)
    swap(second, third);


Look at this example:
1 2 3

With yours you will get
first < second? true - 2 1 3
third < second? false - 2 1 3
first < third? true - 3 1 2

If you did my method it would be
first < second? true - 2 1 3
first < third? true - 3 1 2
second < third? true - 3 2 1


By the way line 40 should be obj.mySort(); since you are calling a function.
Then line 41 should be std::cout << first << ',' << second << ',' << third << std::endl;
Last edited on
Oh, yes, that's right! I feel oblivious. Haha.

However, after fixing those errors I am greeted with two new errors:
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
Error 2 error LNK1120: 1 unresolved externals

Is this still an issue with my code?
Yeah, didn't notice this earlier. Kind of looks like you come from a java background because your main function is inside the class.

move your main outside of the class.


Generally speaking you would have 3 files, File 1) a header that has the definitions of the functions/class. file 2) the declarations of the functions/class, file 3) the main source file that will create the objects/call the functions.

But if you just moved the main function after the class definitions/declarations it should compile in 1 file.

here is what I mean

1
2
3
4
5
6
7
class Test
{
};

int main() //notice how it is not inside the class
{
}


By the way I didn't notice before but line 38 should be cin >> first >> second >> third; the comma operator does not do what you think it does.
Last edited on
That all worked very well!! Thank you so much for your help! :)

Here is the final code I ended up with:

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
#include <iostream>
using namespace std;

class Sort3
{
private:
	float& first;
	float& second;
	float& third;

public:
	Sort3(float& a, float& b, float& c) : first(a), second(b), third(c)
	{
	}

	void swap(float& x, float& y)
	{
		float temp;

		temp = x;
		x = y;
		y = temp;
	}
	void mySort()
	{
		if (first < second)
			swap(first, second);
		if (first < third)
			swap(first, third);
		if (second < third)
			swap(second, third);
		return;
	};

};
int main()
{
	float first, second, third;
	std::cout << "Please enter three integers." << endl;
	cin >> first >> second >> third;
	Sort3 obj(first, second, third);
	obj.mySort();
	std::cout << first << ', ' << second << ', ' << third << std::endl;
	return 0;
}
Topic archived. No new replies allowed.