ordering numbers

hey guys im doing an assignment where if you type in 3 numbers out of order ie. 4 5 4 it should return 4 4 5 this is what i've got so far im able to get it to order them if they're backwards but if they're mixed up i cant get them to re arrange this is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;


int main()
{
	string answer;
	string answer2;
	string answer3;
	cout <<"Enter 3 Numbers: " << endl;
	cin >> answer >> answer2 >> answer3;
	cout << "you entered: " << answer3 << "," << answer2 << "," << answer << endl;
	
	if(answer2 <= answer && answer3 <= answer2 == true);
	{
		return false;
	}

	return 0;

}
Last edited on
Read them into a std::vector and std::sort them. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::cout << "Enter 3 numbers:" << std::endl;
std::vector<int> v;
int n;
while(std::cin >> n && v.size() < 3)
{
	v.push_back(n);
}
std::sort(v.begin(), v.end());
std::cout << "You entered:";
for(std::vector<int>::size_type i = 0; i < v.size(); ++i)
{
	std::cout << ' ' << v[i];
}
std::cout << std::endl;
hey man i tried that and it says std::sort isnt in the std library for some reason and i did #include <vector>

#include <algorithm>

When in doubt about something like this, use this site's reference.
i got it running but it doesent display the number they entered :s sorry man i've been struggling with this basic stuff idk why........
Can you post your entire program?
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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;


int main()
{
	std::cout << "Enter 3 numbers:" << std::endl;
std::vector<int> v;
int n;
while(std::cin >> n && v.size() < 3)
{
	v.push_back(n);
}
std::sort(v.begin(), v.end());
std::cout << "You entered:";
for(std::vector<int>::size_type i = 0; i < v.size(); ++i)
{
	std::cout << '  ' << v[i];
}
std::cout << std::endl;

}


btw thanks for all your help man even with the windows programming stuff i really appreciate it
Last edited on
Line 14 needs to be changed:

while(v.size() < 3 && std::cin >> n )

because the size of the vector needs to be evaluated before we try to read again. And you're welcome.
Last edited on
You might also want to check how to implement your own sorting algorithm, always good for training.

http://www.concentric.net/~ttwang/sort/sort.htm

Start with bubble sort and then check the rest :)


Last edited on
it works now thanks but
when i enter the 3 numbers when it out puts the entries to the console it appears like this
ie. Entered 4 5 4
it couts

8224
8224
8225


so i commented line 23 out


std::cout << ' ' << v[i] << std::endl;

and it displays only the numbers entered

thanks alot man i really appreciate it

Last edited on
You put two spaces inside the single quotes (up in the other post).
Topic archived. No new replies allowed.