array
Nov 12, 2013 at 11:23am UTC
can someone help me with this number sorter, i want the user to input 5 number and pick between ascending or descending order, i dont no the code, please help?
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 57 58 59 60 61
// Maths Helper
#include <iostream>
using namespace std;
char menu()
{
char choice;
cout<< "\nNumber Sorter - Created By Ben Tickle" ;
cout<< "\n\n**************************************\n" ;
cout<< "\n\n Please choose one of the following:\n" ;
cout<< "\n\n 1 - Ascending order " ;
cout<< "\n 2 - Descending order " ;
cout<< "\n 3 - Exit" ;
cout<< "\n\n\n**************************************\n" ;
cout<< "\n\n Enter you choice and press return: " ;
cin >> choice;
return choice;
}
int main()
{
int mynum;
char choice;
do
{
choice = menu();
switch (choice)
{
case '1' :
cout << "\n\nAscending order"
<< "\n\n\nPlease enter 5 numbers include spaces:" ;
cin >> mynum;
// Pause for user
system("pause" );
break ;
case '2' :
cout<< "\n\nDescending order"
<< "\n\n\nPlease enter 5 numbers include spaces:" ;
cin>>radius;
// Pause for user
system("pause" );
break ;
case '3' :
cout<< "Exit" ;
break ;
default :
cout<< "\nNot a valid choice." ;
}
} while (choice != '3' );
return 0;
}
Nov 12, 2013 at 11:35am UTC
You can use an
std::list or
std::vector of integers.
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
#include <iostream>
#include <list>
// ...
std::list<int > numbers;
int count=5;
for (int i=0; i < count; ++i) // reading...
{
int n;
std::cout << "Read number " << i + 1 << ": " ;
std::cin >> n;
numbers.push_back(n);
}
// ...
numbers.sort(); // sorting...
// ...
// printing...
for (std::list<int >::const_iterator ci = numbers.begin(); ci != numbers.end(); ++ci)
std::cout << *ci << ' ' ;
std::cout << std::endl;
Last edited on Nov 12, 2013 at 11:36am UTC
Nov 12, 2013 at 11:39am UTC
how can i fit that into my code, im a beginner so new to this, and the is a errors on line 9
Nov 12, 2013 at 11:53am UTC
and the is a errors on line 9
To clear up what the OP refers to: originally on line 9, I mistakenly left
while() instead of
for() . Hence the edit... which I thought would be quick enough.
how can i fit that into my code, im a beginner so new to this
It is time for you to make progress.
http://www.cplusplus.com/doc/tutorial/
Nov 12, 2013 at 12:01pm UTC
i really dont no what you have just said
Topic archived. No new replies allowed.