Aug 12, 2010 at 8:24pm UTC
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 Aug 12, 2010 at 8:26pm UTC
Aug 12, 2010 at 9:38pm UTC
hey man i tried that and it says std::sort isnt in the std library for some reason and i did #include <vector>
Aug 12, 2010 at 9:41pm UTC
#include <algorithm>
When in doubt about something like this, use this site's reference.
Aug 12, 2010 at 9:48pm UTC
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........
Aug 12, 2010 at 9:50pm UTC
Can you post your entire program?
Aug 12, 2010 at 10:03pm UTC
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 Aug 12, 2010 at 10:03pm UTC
Aug 13, 2010 at 3:14am UTC
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 Aug 13, 2010 at 3:14am UTC
Aug 13, 2010 at 9:47am UTC
Last edited on Aug 13, 2010 at 9:49am UTC
Aug 13, 2010 at 12:17pm UTC
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 Aug 13, 2010 at 12:29pm UTC
Aug 13, 2010 at 2:36pm UTC
You put two spaces inside the single quotes (up in the other post).