0
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

bool comp(int a, int b){
    return a < b;
}

int main(int argc, char* argv[]){
    char array[argc-1];
    for(int i = 1; i < argc; i++){
        array[i-1] = *argv[i];
    }
    for(int j = 0; j < argc; j++){
        cout<<array[j]<<" ";
    }
    std::sort(array, array+argc-1, comp);
    for(int j = 0; j < argc; j++){
        cout<<array[j]<<" ";
    }
    cout<<endl;
    return 0;
}

This code is supposed to sort the arguments of the command line. But when I launch it:

.\a.exe 11 21 34 9 87

I get this output:

1 2 3 8 9
anastaciu
  • 23,467
  • 7
  • 28
  • 53
G0053
  • 19
  • 1
  • 5
    You copy only the first character of each argument. – Yksisarvinen Oct 23 '20 at 11:15
  • 4
    Yes, you get this output because this is what your program does. It sorts only the first character of each parameter. This is literally what your program does. If you want it to do something else, you will need to change your program accordingly. Does it make sense to you that your comparison function compares only two `int` values? How do you expect to sort character strings when the actual comparison only compares two `int` values? You can start with this part of the puzzle, and work your way from there. – Sam Varshavchik Oct 23 '20 at 11:15
  • 1
    Maybe [this](https://stackoverflow.com/questions/41887068/sort-command-line-args-in-c) helps – Jabberwocky Oct 23 '20 at 11:19
  • Does this answer your question? [Sort command line args in C++](https://stackoverflow.com/questions/41887068/sort-command-line-args-in-c) – Ranoiaetep Oct 23 '20 at 11:20
  • 1
    `array` is an array of `char`, but your comparison function compare two `int` values. – Some programmer dude Oct 23 '20 at 11:20
  • Besides all that has been brought up already, please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then learn how to [edit] your questions to improve them, like for example actually *asking* a question. And tell us what problem the code is supposed to solve (possibly copy-paste the whole exercise/assignment including *all* limitations and requirements). – Some programmer dude Oct 23 '20 at 11:22
  • 1
    Variable length arrays are not C++ standard compliant, you should maybe use `std::vector` – anastaciu Oct 23 '20 at 11:24
  • Thanks for eachother. I solved this problem by using atoi() in pushing to array loop. – G0053 Oct 23 '20 at 12:35
  • @G0053 `atoi` is dangerous, in case of overflow the behavior is undefined, `std::stoi` would be a better way. I'll answer. – anastaciu Oct 23 '20 at 12:37

1 Answers1

0

Variable length arrays are not C++ standard compliant, you should instead use std::vector

In this case you wouldn't even need to do so, you can simply sort argv, which is nothing more than an array of strings, in place:

bool comp(const char* a, const char* b)
{
    return std::stoi(a) < std::stoi(b); // convert to number and compare
}

int main(int argc, char *argv[])
{
    try //if argument is not parseable, exception is thrown
    {
        std::sort(&argv[1], &argv[argc], comp);
    }
    catch (std::exception &e)
    {
        //error parsing argument handling
        std::cerr << "Argument not parseable " << e.what() << "\n";
        return EXIT_FAILURE;
    }

    for (int j = 1; j < argc; j++)
    {
        std::cout << argv[j] << " ";
    }
}

The comparator function could also be a lambda which I believe you have an example in one of the links provided in the comments.

Input:

.\a.exe 11 21 34 9 87

Output:

9 11 21 34 87 

Bad input:

.\a.exe 11 21 34 9 87 gjh

Output:

Argument not parseable stoi
anastaciu
  • 23,467
  • 7
  • 28
  • 53