I want to make a program by using the command line to descending numbers with an input of five numbers or below on Visual studios 2019, if the input of numbers were six or above which is the limit, then an error output will occur and tells you to try again for input of numbers.
Here is the document of how the output should be with the command line on Visual studios
Here is my coding which isnt a command line to descending numbers with five numbers from the input as I used it just for the idea how to do it to descending order of input numbers. However I can only figure out to descending five numbers only and it wont work if I type four numbers or below of the input, not to mention i cant figure out how to make a limit for the input
#include <iostream>
usingnamespace std;
usingnamespace std;
int main ()
{
int number[5];
int i, j, descending;
cout<<"\n Enter 5 or below Numbers for descending order : \n";
for (i = 0; i < 5; ++i) // input of five numbers or below.
cin>>number[i];
for (i = 0; i < 5; ++i) // sort numbers in descending order with 5 numbers or below
{
for (j = i + 1; j < 5; ++j)
{
if (number[i] < number[j])
{
descending = number[i];
number[i] = number[j];
number[j] = descending;
}
}
}
cout<<"\n Numbers in Descending Order : \n";
for (i = 0; i < 5; ++i)
{
cout<<" ";
cout<<number[i];
}
}
Keep in mind the program should be a beginners level and dont make it complicated so it can be easier to read as a beginner
#include <iostream>
#include <utility>
int main() {
int number[5] {};
int cnt {};
std::cout << "\n Enter 5 or below Numbers for descending order : \n";
for (int no; std::cin >> no; ++cnt)
if (cnt < 5)
number[cnt] = no;
if (cnt > 5)
return (std::cout << "You cannot input more than 5 numbers\n"), 1;
for (int i = 0; i < cnt; ++i) // sort numbers in descending order with 5 numbers or below
for (int j = i + 1; j < cnt; ++j)
if (number[i] < number[j])
std::swap(number[i], number[j]);
std::cout << "\n Numbers in Descending Order : \n";
for (int i = 0; i < cnt; ++i)
std::cout << " " << number[i];
std::cout << '\n';
}
A more complicated but without requiring ctrl-z to terminate input is:
#include <iostream>
#include <utility>
#include <string>
#include <sstream>
int main() {
int number[5] {};
int cnt {};
std::string input;
std::cout << "\nEnter 5 or below Numbers for descending order : \n";
std::getline(std::cin, input);
std::istringstream iss(input);
for (int no; iss >> no; ++cnt)
if (cnt < 5)
number[cnt] = no;
if (cnt > 5)
return (std::cout << "You cannot input more than 5 numbers\n"), 1;
for (int i = 0; i < cnt; ++i) // sort numbers in descending order with 5 numbers or below
for (int j = i + 1; j < cnt; ++j)
if (number[i] < number[j])
std::swap(number[i], number[j]);
std::cout << "\nNumbers in Descending Order : \n";
for (int i = 0; i < cnt; ++i)
std::cout << number[i] << ' ';
std::cout << '\n';
}
1) Ctrl-z is the simpler as it doesn't use string/stringstream. You asked for non-complicated! If exactly 5 (or another number) of numbers are required to be input then that again simplifies things. But inputting a variable number of numbers isn't quite as straightforward.
None of the programs posted in this post are using command line arguments to supply the required numbers, including the OP's code.
Note it would have been much easier if the OP would have cut and pasted the assignment instead of posting the link.
To use command line arguments main() must be in the form of int main(int argc, char** argv). But note that when using command line arguments argv is an array of C-strings, which could be almost anything not just numbers which can complicate things somewhat. And don't forget that argument 0 is usually the program name which probably shouldn't be counted when determining the number of arguments.
#include <iostream>
#include <utility>
#include <cstdlib>
int main(int argc, char* argv[]) {
if (argc < 2)
return (std::cout << "No numbers specified\n"), 1;
if (argc > 6)
return (std::cout << "You cannot input more than 5 numbers\n"), 2;
int number[5] {};
int cnt {};
for (int i {1}; i < argc; ++i)
number[i - 1] = atoi(argv[i]);
for (int i = 0; i < argc - 1 ; ++i) // sort numbers in descending order with 5 numbers or below
for (int j = i + 1; j < argc - 1; ++j)
if (number[i] < number[j])
std::swap(number[i], number[j]);
std::cout << "\nNumbers in Descending Order : \n";
for (int i = 0; i < argc - 1; ++i)
std::cout << number[i] << ' ';
std::cout << '\n';
}
c:\MyProgs>test64 1 2 3 4 6
Numbers in Descending Order :
6 4 3 2 1