Command line about descending numbers using input numbers

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

https://docs.google.com/document/d/1xHg_-1hT97IsT-_8a7U2gTGRsn7mLbVLJ3XDe_fBilc/edit?usp=sharing

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

using namespace 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
Last edited on
Simply you can do this. But the input needs to be terminated with a ctrl-z (end-of-input):

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
#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:

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
#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';
}

Last edited on
@seeplus I have two questions to ask

1) Why there is one program require to (crtl + z) to terminate the output?

2) Are these programs based on command-lines just like the document that I had mentioned, something like (int argc, char * argv[])?
Last edited on
2) Yes.

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.
@seeplus

Thanks for answering these questions.
I will try to test more about the command lines for practice
Last edited on
@seeplus

Sorry but I want to ask one more question according to the output from the linked document, why the document output appears
list \command_line_arg_tut \ debug)Command_line_arg_tut 
as it used command line to descend the input numbers ?

Since I did saw some youtube tutorials about command line, some has these output while some dont
Last edited on
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.






Oh. It's supposed to use command-line arguments for the program. Well in that case consider (no input checking except number of args):

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 <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

Last edited on
Topic archived. No new replies allowed.